Search code examples
cfunctionmultidimensional-arrayimplicit-conversionfunction-call

passing argument 1 of 'a function' makes pointer from integer without a cast


Probably a really easy question, but I'm trying to make a tic-tac-toe engine and this is the code (putting it later because despite me closing the triple graves it still puts it in code block if below for some reason)

I get warnings for: [Warning] passing argument 1 of 'resetboard' makes pointer from integer without a cast

[Warning] passing argument 1 of 'getnewboardO' makes pointer from integer without a cast

[Warning] passing argument 1 of 'printboard' makes pointer from integer without a cast

I'm a beginner, but I've worked with 2d bool arrays in nearly the same way in another project so I'm stumped as to what I'm doing wrong.


void getnewboardO(char board[3][3])
{
    char inputrow;
    
    int inputcoloumn;
    
    scanf("%c%d", &inputrow, &inputcoloumn);
    
    inputcoloumn--;
    
    if(inputrow=='A')
        board[0][inputcoloumn]='O';
    
    if(inputrow=='B')
        board[1][inputcoloumn]='O';
        
    if(inputrow=='C')
        board[2][inputcoloumn]='O';
}

void resetboard(char board[3][3])
{
    int i, j;
    
    for (i=0;i<2;i++)
        for (j=0;j<3;j++)
            board[i][j]='_';
    for (j=0;j<3;j++)
        board[2][j]=' ';
}

void printboard(char board[3][3])
{
    printf("%c|%c|%c\n%c|%c|%c\n%c|%c|%c",board[0][0], board[0][1], board[0][2], board[1][0], board[1][1], board[1][2], board[2][0], board[2][1], board[2][2]);
}


main()
{
    char board[3][3];
    
    resetboard(board[3][3]);
    
    printf("_|_|_\n_|_|_\n | |");
    
    getnewboardO(board[3][3]);
    
    printboard(board[3][3]);
}

Solution

  • Let's for example consider this function declaration.

    void getnewboardO(char board[3][3]);
    

    The compiler will adjust the parameter having array type to pointer to the array element type.

    That is this function declaration is equivalent to

    void getnewboardO(char ( *board )[3]);
    

    You could even declare the function like

    void getnewboardO(char board[100][3]);
    

    or

    void getnewboardO(char board[10][3]);
    

    in any case the both function declarations are adjusted by the compiler to the function

    void getnewboardO(char ( *board )[3]);
    

    However in the function call

    getnewboardO(board[3][3]);
    

    you are using a non-existent element of the array board of the type char because the valid range of the both indices is [0, 3).

    That is this expression used as an argument

    board[3][3]
    

    does not denote the declared array board. it denotes a scalar non-existent object of the type char that due to the integer promotions in turn is converted to the type int.

    You need to pass the whole array to the function. That is what you need to write is

    getnewboardO(board);
    

    In this case the array designator will be converted to pointer to its first element of the type char ( * )[3] as the type of the function parameter.