2

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]);
}
dbush
  • 205,898
  • 23
  • 218
  • 273
AnProg
  • 43
  • 1
  • 6
  • `resetboard(board[3][3]);` ==> `resetboard(board);` The `board[3][3]` is an integer `char` value (but with out-of-range indexing), not the (pointer to the) array itself. – Weather Vane May 24 '21 at 19:40
  • uh I must admit I kinda did what you said out of trust but it gave me a ton of [Error] subscripted value is neither array nor pointer nor vector – AnProg May 24 '21 at 19:46
  • 1
    @AnProg you didn't by any chance change `char board[3][3];` to `char board;` too or change the function signatures, did you? – mediocrevegetable1 May 24 '21 at 19:47
  • 2
    There are two other occurrences to change too (calls to the other two functions). My test program compiles cleanly. Please note that I advised changing the call `resetboard(board[3][3]);` NOT `void resetboard(char board[3][3])`. – Weather Vane May 24 '21 at 19:48
  • I'm an idiot sorry it compiles without warnings now – AnProg May 24 '21 at 19:52

1 Answers1

0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335