Trying to teach myself how to pass an array from int main() to a function and I just don’t understand it.
Here is my code.
#include <stdio.h>
void printboard( char *B ) {
/*Purpose: to print out the tic tac toe board B
*/
int i,j;
printf("\n");
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
printf( " %c ",B[i][j]);
}
printf("\n\n");
}
}
int main( void ) {
char B[3][3] = { '-','-','-',
'-','-','-',
'-','-','-' };
printboard(B);
}
I get this error:
test.c: In function 'printboard':
test.c:12: error: subscripted value is neither array nor pointer
test.c: In function 'main':
test.c:25: warning: passing argument 1 of 'printboard' from incompatible pointer type
Just need to get an understand of how pointers work and are passed so I can go ahead with my work.
Two dimensional array decays to a pointer to one dimensional array (i.e., (*)[] )
So, change from –
to
Also, two dimensional array initialization should be done this way –
Check Results