Hi,
If I am not wrong when we declare a 2D (or multiD) array like "char x[3][4]" then it is reserved 3x4=12 contiguous bytes from the memory.
But we can create a 2D array with dynamically allocation of memory.In this case I would like someone to confirm me that it is not certain that the memory locations will be contiguous.
The code for dynamically create a 2D array is:
Code:
#include <stdlib.h>
void foo ( ) {
int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL) {
fprintf(stderr, "out of memory\n");
/*exit or return*/
}
for(i = 0; i < nrows; i++) {
array[i] = malloc(ncolumns * sizeof(int));
if(array[i] == NULL) {
fprintf(stderr, "out of memory\n");
/*exit or return*/
}
}
}