I was trying to make a 2D-matrix with sizes of both dimensions selected by the user (i.e. the dims[] values were assigned through scanf), and then initialize it.
My program compiles but crashes when I try to assign values to a char matrix using a function.
I guess it has something to do with the number of columns (the second brackets) and the fact that I used an array to define the size in two dimensions of the matrix. The problem only occurs when assigning values (and not, say, when printing).
int main()
{
int dims[2] = {3,4};
//^The exact values are besides the point, chose some at random
char board[dims[0]][dims[1]];
initialize_board(board, dims);
}
The function looks like this (MAX_SIZE is #defined as 25):
void initialize_board(char board[][MAX_SIZE], int board_side[])
{
for(int i=0; i<board_side[0]; i++)
{
for(int j=0; j<board_side[1]; j++)
{
board[i][j]='-';
}
}
}
The function is declared like this:
void initialize_board(char board[][MAX_SIZE], int board_side[]);
What should I do to fix this?