开发者

Changing pointer to a struct, passed by reference

I have this call on a file called 'PlayBoard.c':

MoveSucc = putBoardSquare(theBoard, getX, getY, nextTurn);

Where 'theBoard' is a pointer to struct Board. Inside the function I am changing the board's size by referencing the pointer to ANOTHER Board struct, a bigger one. Will it change 'theBoard' on 'PlayBoard.c', where MoveSucc is invoked?

EDIT: putBoardSquare is defined in another source file

EDIT: I've added the relevant functions

Boolean putBoardSquare(BoardP theBoard, int X, int Y, char val)
{
    if (val != 'X' && val != 'O')
    {
        reportError(BAD_VAL);
        return FALSE;
    }

    if (X<0 || Y<0)
    {
        reportError(OUT_OF_BOUND);
        return FALSE;
    }

    if (X>theBoard->height || Y>theBoard->width)
    {
        theBoard = expandBoard(theBoard, X,Y);
    }

    printf("BOARD SIZE IS %d*%d\n",theBoard->height,theBoard->width);

    if (theBoard->board[X][Y] == 'X' || theBoard->board[X][Y] == 'Y' )
    {
        reportError(SQUARE_FULL);
        return FALSE;
    }

    if (val != turn)
    {
        reportError(WRONG_TURN);
        return FALSE;
    }

    theBoard->board[X][Y] = val;
    printf("PUT %c\n",theBoard->board[X][Y]);
    changeTurn(val);

    return TRUE;
}

static BoardP expandBoard(ConstBoardP theBoard, int X, int Y)
{
    int newWidth = theBoard->width;
    int newHeight = theBoard->height;
    if (X>theBoard->height)
    {
        newHeight = (newHeight+1) * 2;开发者_JS百科
    }

    if (Y>theBoard->width)
    {
        newWidth = (newWidth+1) * 2;
    }

    BoardP newBoard = createNewBoard(newWidth,newHeight);
    copyBoard(theBoard,newBoard);
    printf("RETUNRNING NEW BOARD OF SIZE %d*%d\n",newHeight,newWidth);
    return newBoard;
}

As you can see, when the user tries to place 'X' or 'O' outside the board, it needs to be expanded which happens (I know cause I've printed new board's size in expandBoard() and in putBoardSquare()). But the pointer in 'PlayBoard.c' doesn't seem to change anyway....

My question: how can I change the pointer of a struct passed as an argument to another function? In 'PlayBoard.c' I pass one struct as an argument, and I want putBoardSquare to refrence it to another struct, which will take effect in PlayBoard.c as well.

Am I clear?


EDIT

theBoard = expandBoard(theBoard, X,Y);

This assignment only changes a local variable. You'll have to add one level of indirection, as in:

MoveSucc = putBoardSquare(&theBoard, getX, getY, nextTurn);

Boolean putBoardSquare(BoardP *theBoard, int X, int Y, char val)
{
    /* ... */
    *theBoard = expandBoard(theBoard, X,Y);
    /* ... */
}

Your question is confusing (perhaps you should post the code you have), but the error you have is cause simply by the definition of the struct not being available in PlayBoard.c. For instance, if you only have

struct foo;
void foo(struct foo *foov) { ... }

without a definition of foo available, as in

struct foo { int a; ... }

then you won't be able to access the members of the structure (see "opaque type").


If I understand correctly and you want to change where theBoard points to, you need to define it as a pointer to pointer, not as pointer.

MoveSucc = putBoardSquare(&theBoard, getX, getY, nextTurn);

and change the parameter in putBoardSquare() to ** and when you set the pointer do it like (assuming x is a pointer):

*theBoard = x;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜