过去,我有一个叫做“PlayBoard.c”文件这一呼吁:改变指针结构,通过引用
MoveSucc = putBoardSquare(theBoard, getX, getY, nextTurn);
其中“theBoard”是结构板的指针。在函数内部,我通过引用指向另一个Board结构体的指针来更改电路板的大小。它会改变'PlayBoard.c'上的'theBoard',MoveSucc被调用吗?
编辑:putBoardSquare在另一个源文件中定义
编辑:我已经添加了相关功能
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;
}
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;
}
正如你可以看到,当用户试图放置“X”或“O '在董事会之外,它需要扩展发生(我知道因为我已经在expandBoard()和putBoardSquare()中打印了新的董事会的大小。但'PlayBoard.c'中的指针似乎没有改变...
我的问题:我如何更改作为参数传递给另一个函数的结构的指针?在'PlayBoard.c'中,我将一个结构作为参数传递,并且我希望putBoardSquare将其引用到另一个结构中,该结构也将在PlayBoard.c中生效。
我清楚吗?
请提供更多相关代码。这个问题目前的形式很难回答。 –
我删除了我的答案,因为它回答了错误的问题,但指针没有更新的问题是因为您需要一个指向board的指针的指针(假设BoardP是一个简单的指针)。 – Makis
你能详细解释一下吗?我需要一个指针指针? 我认为当你传递一个指针作为函数参数时,改变它将在函数外生效 – yotamoo