0

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.

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;
}
6
  • int getNextMove(move *nextMove) { --> move *getNextMove(move *nextMove) { also return 0; --> return NULL; Commented Oct 17, 2014 at 14:50
  • Well, you have a function with a return type of int and you're returning a move* from it. That's not going to work. Commented Oct 17, 2014 at 14:50
  • 1
    The return type of a function is not a guideline, it's a rule. If your function has a return type int, then that's what it should return. have it return a move *, or int , and change the calling code accordingly Commented Oct 17, 2014 at 14:53
  • The choice of words used in the function documentation is unfortunate, and contributes to confusion. It is more common to describe at least three categories: input:..., output:,... and return:... With output in your code being described as struct move *, and return being described as 1 for success, 0 for failure, (or whatever your preference is) Commented Oct 17, 2014 at 15:06
  • Besides it's actually pointless to return nextMove with your implementation, because after invoking move *move1 = getNextMove(move2), move1 will be the same as move2. In your case return nextMove; should be simply replaced by return 1;. And you should invoke getnextMove like this int continuegame = getNextMove(mynextmove). Commented Oct 17, 2014 at 15:13

3 Answers 3

4

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.

nextMove is a move*, not an int, hence the warning. 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.

Sign up to request clarification or add additional context in comments.

Comments

2

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:

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 * getMove(void)
{
    static move moveToReturn;

    /* some operations on the move stucture */

    return &moveToReturn
}

1 Comment

Thanks, I didn't realize that "return the coordinates for the next move" was "automatically done" via the scanf. Thanks for clarifying :)
1

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 * getNextMove(move * nextMove) {
    ...
    return nextMove;
}

1 Comment

Note, too, that returning the move * is contrary to the documentation of the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.