tags:

views:

220

answers:

4

I have the following function

void initBoard(int * board[BOARD_ROWS][BOARD_COLS]){
  int z = 0;
  for( z = 0; z<10; z+=1){
     int l;
     for( l = 0; l<10; l+=1){
        board[z][l] = 0;
     }
  }
}

and from main i call it like

  int plBoard[10][10];
  initBoard(&pcBoard);

when compiling it works but i get a warning saying: warning: passing argument 1 of 'initBoard' from incompatible pointer type. array is an integer and and function expects a int pointer i am passing the address of int. What is wrong with it?

+1  A: 

I'm not entirely sure, but I believe it's because:

initBoard is expecting an array of array of pointer to integers.

However, plBoard is a pointer to an array of array of integers

Smashery
so what would be the correct signature for the function?
Hamza Yerlikaya
Pax has it, methinks.
Smashery
I like to do it like this: void initBoard(int board[/*BOARD_ROWS*/][BOARD_COLS]); to avoid confusion. Showing the size one expect, but also showing that one couldn't use sizeof on it (since it really is a pointer instead of an array, in the parameter list.
Johannes Schaub - litb
I think the correct parameter is: int (* board) [BOARD_ROWS][BOARD_COLS]
Steve Melnikoff
+12  A: 

Apart from the obvious typo in your question (the definition of plBoard but the use of pcBoard), you don't need to pass arrays with the address-of operator (&), since they're converted to addresses automatically.

But your incompatible type problem is caused by the use of:

int * board[BOARD_COLS][BOARD_ROWS]

which is actually a 2d array of int pointers (technically an array of array of pointer to int), not integers as you expected.

You should use:

void initBoard(int board[BOARD_COLS][BOARD_ROWS]) {  // <-- remove "*"
: : :
}
int plBoard[10][10];
initBoard(plBoard);                                  // <-- remove "&"
paxdiablo
A: 

Technically you're sending the memory address of a pointer into initBoard(). When you declare an array (in this case plBoard), it points to a memory address where the contiguous memory holds your data items... so it's redundant to say this...

int plBoard[10][10];  initBoard(&pcBoard);

Pax has your answer, I was just clairifing becauase you haven't marked it as the answer yet.

Balk
A: 

Just to be on the same page .. I totally agree with Pax ..

Blockquote int pcBoard[10][10]; Blockquote initBoard(&pcBoard);

Now suppose pcBoard[0][0] points to 101th location (32-bit address) and so on and so forth.. What you are passing by meaning &pcBoard is the 32-bit address of pcBoard[0][0]. So when the function

Blockquote void initBoard(int * board[BOARD_ROWS][BOARD_COLS]){

Uses it..it displays an error saying that it is expecting a two dimensional array whereas what it is getting in a 32-bit address which is 4 bytes(int).

Just a hint, you do not need to specify both board[BOARD_ROWS][BOARD_COLS] when passing.. you can conveniently eliminate BOARD_ROWS and just specify BOARD_COLS.

Thunderboltz