简体   繁体   English

更改指向结构的指针,通过引用传递

[英]Changing pointer to a struct, passed by reference

I have this call on a file called 'PlayBoard.c':我对一个名为“PlayBoard.c”的文件进行了调用:

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

Where 'theBoard' is a pointer to struct Board.其中 'theBoard' 是指向 struct Board 的指针。 Inside the function I am changing the board's size by referencing the pointer to ANOTHER Board struct, a bigger one.在 function 内部,我通过引用指向另一个更大的板结构的指针来更改板的大小。 Will it change 'theBoard' on 'PlayBoard.c', where MoveSucc is invoked?它会改变调用 MoveSucc 的“PlayBoard.c”上的“theBoard”吗?

EDIT: putBoardSquare is defined in another source file编辑: putBoardSquare 在另一个源文件中定义

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;
    }

    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()).如您所见,当用户尝试将“X”或“O”放置在板外时,需要对其进行扩展(我知道因为我已经在 expandBoard() 和 putBoardSquare() 中打印了新板的尺寸) . But the pointer in 'PlayBoard.c' doesn't seem to change anyway....但是'PlayBoard.c'中的指针似乎并没有改变......

My question: how can I change the pointer of a struct passed as an argument to another function?我的问题:如何更改作为参数传递给另一个 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.在“PlayBoard.c”中,我将一个结构作为参数传递,我希望 putBoardSquare 将其引用到另一个结构,这也将在 PlayBoard.c 中生效。

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 .您的问题令人困惑(也许您应该发布您拥有的代码),但是您遇到的错误仅仅是由于PlayBoard.c中不可用的结构定义造成的。 For instance, if you only have例如,如果你只有

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

without a definition of foo available, as in没有可用的 foo 定义,如

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.如果我理解正确并且您想更改theBoard指向的位置,则需要将其定义为指向指针的指针,而不是指针。

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):并将putBoardSquare()中的参数更改为**并在设置指针时执行此操作(假设x是指针):

*theBoard = x;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM