reading a few articles on dynamic memory allocation for multidimensional arrays in c, I came across the following code snippet: (article link)
#define COLS 5
int (*rptr)[COLS];
int main(void)
{
int nrows = 10;
int row, col;
rptr = malloc(nrows * COLS * sizeof(int));
for (row = 0; row < nrows; row++)
for (col = 0; col < COLS; col++)
rptr[row][col] = 17;
return 0;
}
Here, a multidimensinal array is defined as int (*rptr)[COLS] and then dynamically linked to malloc(nrows * COLS * sizeof(int)).
Now the question is that how does the pointing work, eg: if the pointing address start from 100,101,102...etc
(assuming int takes 1 byte for simplicity)
rptr[0] points to -> 100
rptr[1] points to -> 105
Does the compiler internally link the address accordingly based on the column size as soon as malloc(nrows * COLS * sizeof(int)) is executed?
Thanks