ClockwerkSC
ClockwerkSC

Reputation: 45

Repeated errors when passing arguments in functions

In my program I have encountered the same 2 errors countless times. Here's a sliver of the program to illustrate the problem:

void update_table(char ary[SIZE][SIZE], int row, int column, char token)
{

if(token == 'X')
    ary[row][column] = 'X';
else if(token == 'O')
    ary[row][column] = 'O';


}

...

void generate_player2_move(char a[SIZE][SIZE], int rw, int cn)
{
char type = 'X';
srand(time(NULL));
rw = rand() % 3;
cn = rand() % 3;


    while(check_legal_option(a[SIZE][SIZE], rw, cn) == 1)
    {

    rw = rand() %3;
    cn = rand() %3;

    }
printf("Player 2 has entered [row, col]: %d,%d", rw, cn);
update_table(a[SIZE][SIZE], rw, cn, type);
display_table(a[SIZE][SIZE]);
}

and these are the two types of errors I have been getting:

note: expected ‘char (*)[3]’ but argument is of type ‘char’
 void update_table(char ary[SIZE][SIZE], int row, int column, char token)

and

  warning: passing argument 1 of ‘update_table’ makes pointer from integer without a cast [-Wint-conversion] update_table(a[SIZE][SIZE], rw, cn, type);

I am confused about the first error, because I thought the argument is char * [3], and then I don't quite understand the second error.

Upvotes: 1

Views: 62

Answers (2)

D. Pante
D. Pante

Reputation: 381

The parameter to your generate_player2_move function is declared as char a[SIZE][SIZE]. Apparently you are trying to pass a 2 dimensional array of chars. However, when you pass an array you are actually passing a pointer to the 1st element in the array. You are not passing the array itself. Therefore, because a 2 dimensional array is really an array of arrays, the declaration of the parameter should really be a pointer to an array an array of SIZE characters. The parameter should be declared as follows: char (*a)[SIZE]

Upvotes: 1

frslm
frslm

Reputation: 2978

You're passing in a[SIZE][SIZE] instead of just a when calling check_legal_option(), update_table(), and display_table().

a[SIZE][SIZE] attempts to access a's element in row SIZE and column SIZE, which would take you out of bounds; you only need to declare SIZE in any function declaration that accepts a as a parameter.

Upvotes: 1

Related Questions