I have a static array that I want to use to initialize a dynamic array using pointers.
Right now I have this:
boardinit[BOARD_HEIGHT][BOARD_LENGTH] = {
{-4, -2, -3, -5, -6, -3, -2, -4},
{-1, -1, -1, -1, -1, -1, -1, -1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{4, 2, 3, 5, 6, 3, 2, 4},
};
int main()
{
int board[BOARD_HEIGHT][BOARD_LENGTH];
initboard(board);
for (int j = 0; j < BOARD_HEIGHT; j++)
{
for (int i = 0; i < BOARD_LENGTH; i++)
printf("%d ", board[j][i]);
printf("\n");
}
return 0;
}
void initboard(int (*pboard)[BOARD_HEIGHT])
{
for(int i = 0;i<BOARD_HEIGHT;i++)
pboard[i] = boardinit + i;
}
I want board[BOARD_HEIGHT][BOARD_LENGTH] to initialize as boardinit[BOARD_HEIGHT][BOARD_LENGTH] by passing board to initboard() using pointers, but can't seem to get it to go.
BOARD_HEIGHT/BOARD_LENGTHas fixed constants either via#defineor anenum. And, you want these values to be [global]intdeclarations (i.e. You can prompt the user for the width/height via (e.g.)scanf)???memcpy(board, boardinit, sizeof(board))in themainfunction.expression must be a modifiable lvalueforpboard[i] = boardinit + i;initboardprototype:void initboard(int pboard[BOARD_HEIGHT][BOARD_LENGTH]) { for (int irow = 0; irow < BOARD_HEIGHT; irow++) for (int icol = 0; icol < BOARD_LENGTH; icol++) pboard[irow][icol] = boardinit[irow][icol]; }