I wrote this function to swap values in a multi-dimensional array with my understanding that arrays are pointers.
void
swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
However when I try to use the function
swap(board[d-1][d-2]), board[d-1][d-33];
I get these errors from the compiler and I don’t know why:
fifteen.c: in function 'init':
fifteen.c:166:9: error: passing argument 1 of 'swap' makes pointer from integer without a cast [-werror]
fifteen.c:45:6: note: expected 'int *' but argument is of type 'int'
fifteen.c:166:9: error: passing argument 2 of 'swap' makes pointer from integer without a cast [-werror]
fifteen.c:45:6: note: expected 'int *' but argument is of type 'int'
How do I fix it?
board[d-1][d-2]andboard[d-1][d-33]areint. To swap the both, you have to pass their addresses:If you are using
swap (board[d - 1][d - 2]), &board[d - 1][d - 33]), the instructionint tmp = *a;will try to access to the value on the addressboard[d - 1][d - 2]: this make no sense! Because you are using pointers, you have to pass the address of your variables.