I'm converting Java code to C, and something as simple as swapping contents of multi arrays in Java:
boolean[][] temp = board;
board = nextBoard;
nextBoard = temp;
Seems to be a lot more troublesome in C.
After viewing similar questions on this site, I have learned that I have to use memcpy
which I initiated in a method called arrayCopy
.
This is arrayCopy
:
void arrayCopy(char * a, char * b)
{
struct universe data;
int r;
for(r = 0; r < data.rows; r++)
memcpy(b, a, sizeof(a));
}
Which I call from the main method:
char temp[SIZE][SIZE];
arrayCopy(&data.board, &temp);
arrayCopy(&data.nextBoard, &data.board);
arrayCopy(&temp, &data.nextBoard);
With the following struct:
struct universe
{
char board[SIZE][SIZE];
char nextBoard[SIZE][SIZE];
int columns;
int rows;
}universe;
But I'm getting warnings such as:
A2Q1.c:189:15: warning: incompatible pointer types passing 'char (*)[60][60]' to parameter of type 'char *'
Yet memcpy
only returns pointers, so I can't switch the parameters. I also can't use malloc()
yet as other questions suggest because I have not learned it yet, so any other suggestions would be appreciated.
try this