I have 2D array allocated dynamically using this method: How do we allocate a 2-D array using One malloc statement
#include <stddef.h>
int main() {
size_t i;
unsigned int nrows=2;
unsigned int ncolumns=3;
int **array; /* Declare this first so we can use it with sizeof. */
const size_t row_pointers_bytes = nrows * sizeof *array;
const size_t row_elements_bytes = ncolumns * sizeof **array;
array = malloc(row_pointers_bytes + nrows * row_elements_bytes);
int * const data = array + nrows;
for(i = 0; i < nrows; i++) {
array[i] = data + i * ncolumns;
printf("%x\n", data + (i * 3));
}
}
I understand that array[0]
points to 1st 1D array (of 2D array) and array[1]
points to 2nd 1D array, but now how to initialize (and access) others member of this 2D array, for instance a[0][1]
can be access like?
array[0] + (char *)1
It would great, if I may ask for some graphical view of it.