I would like to make a two dimensional array in C.
For example, I make an int type variable named place like this:
int *place;
I ha
Let place
be a pointer to arrays,
int (*place)[columns] = malloc(rows * sizeof *place);
That gives you a contiguous block of memory (good for locality) that you can simply access with
place[i][j] = whatever;
You need to do the following to allocate arr[x][y] of type int dynamically:
int i = 0;
int **arr = (int**)calloc(x, sizeof(int*));
for(i = 0; i < x; i++)
arr[i] = (int *) calloc(y, sizeof(int));
The idea is to first create a one dimensional array of pointers, and then, for each array entry, create another one dimensional array.
You could use a single 1d array as a 2d array by some smart indexing :
Allocate memory :
place = (int*) malloc(rows * columns * sizeof(int));
To access place[i][j]
, use i and j as :
place[ i*columns + j] = value ;