I am confused about the problem with pointer and array in C.
First, let's see a code snippet:
//case 1 int **a; a = (int **)malloc(sizeof(int*)*m); for(i = 0; i < m; i++) a[i] = (int *)malloc(sizeof(int)*n); //case 2 int b[m][n];
then we knew that the location of b in memory is as follows:
b[0][0] b[0][1] ... ... b[m-1][n-1]
and the memory location is in the following order:
a \ _\| a[0] a[1] ... a[m-1] | | | | | \|/ | a[1][0] a[1][a] ... a[1][n-1] \|/ a[0][1] a[0][2] ... a[0][n-1]
So my question is this: since a[0..m-1][0..n-1] is stored inconsistently in memory, why can we use the subscript operator [] on a ? In other words, why a[i][j] can get the correct element, just like b[i][j] do?
source share