简体   繁体   English

从函数返回指针结构

[英]return a pointer struct from a function

I'm trying to return the user input of which row and column to 'switch' (it's supposed to be a game called Teaser), but I'm not sure how to return the value that I want to. 我试图返回要切换的行和列的用户输入(应该是一个名为Teaser的游戏),但是我不确定如何返回想要的值。

The warning I'm getting is: 我得到的警告是:

 warning: incompatible pointer to integer conversion returning 'move *' from a function with result type 'int' [-Wint-conversion] 
typedef struct {
    int row;
    int column;
} move;

 /* Function:    getNextMove
 * Description: Ask the user for a new move or if the user want to quit
 *              the game.
 * Input:       A pointer to a move structure. Coordinates for the next move
 *              or a signal from the user to end the game.
 * Output:      Return 0 if the user want to end the game, 1 otherwise.
 *              Return the coordinates for the next game through the
 *              structure pointed to.
 */

int getNextMove(move *nextMove) {

    printf("Make a move (Row = 0 ends the game)\n");
    printf("Row = ");
    scanf("%d", &nextMove->row);
    if (nextMove->row == 0)
    {
        return 0;
    }
    printf("Column = ");
    scanf("%d", &nextMove->column);

    return nextMove;
}

You made a simple error. 您犯了一个简单的错误。 The function's documentation says: 该函数的文档说:

Output:      Return 0 if the user want to end the game, 1 otherwise.

But, instead, you're returning 0 or the value of nextMove . 但是,相反,您将返回0nextMove的值。

nextMove is a move* , not an int , hence the warning. nextMovemove* ,而不是int ,因此是警告。 This is also why warnings are so helpful, because they have pointed out this mistake that you have made in returning the wrong thing. 这也是警告如此有用的原因,因为警告指出了您在返回错误内容时所犯的错误。

Change return nextMove to return 1 . return nextMove更改为return 1

You shouldn't return more than one value as suggested by the function banner comment. 如函数标题注释所建议的,您不应返回多个值。 Return the 0 or 1 to indicate game status and the value stored in the address pointed to by nextMove is changed by the scanf() function calls: 返回0或1以指示游戏状态,并通过scanf()函数调用更改nextMove指向的地址中存储的值:

int getNextMove(move *nextMove) {
    printf("Make a move (Row = 0 ends the game)\n");
    printf("Row = ");
    scanf("%d", &nextMove->row);
    if (nextMove->row == 0)
    {
        return 0;
    }
    printf("Column = ");
    scanf("%d", &nextMove->column);

    return 1;
}

For the record if you did want to return a pointer to a move struct an example could be: 对于记录,如果您确实想返回一个指向move结构的指针,则示例可能是:

move * getMove(void)
{
    static move moveToReturn;

    /* some operations on the move stucture */

    return &moveToReturn
}

You are (sometimes) returning the argument. 您(有时)返回参数。 What's the point of that? 有什么意义呢? There's no need to return anything in that case. 在这种情况下,无需返回任何内容。

If you did want to return a pointer to a move , though, then your function should declare that as its return type: 但是,如果您确实想返回一个指向move的指针,则您的函数应将其声明为其返回类型:

move * getNextMove(move * nextMove) {
    ...
    return nextMove;
}

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

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