Firstly, I've already reviewed these:
How are multi-dimensional arrays formatted in memory?
Memory map for a 2D array in C
From there, it is known that a 2D array is not the same as char** but in memory they look exactly same. This sounds strange and so I've researched the following:
#include <stdio.h>
char func(char** m) {
return m[0][0]; //only works for char**, already discussed in the other SO question
}
int main() {
//char a[4][2]; //a 2D char array
int row = 4, col = 2; //char**
char** a = malloc(row * sizeof(char*));
int i;
for (i = 0; i < row; i++) {
a[i] = malloc(col * sizeof(char));
}
//checking the output
printf(" &a = %u\n", &a);
printf(" &a[0] = %u\n", &a[0]);
printf("&a[0][0] = %u\n", &a[0][0]);
printf(" a = %u\n", a);
printf(" a[0] = %u\n", a[0]);
//printf(" a[0][0] = %u\n", a[0][0]); //char value in a[0][0], here a garbage value
//char m = func(a); //only works for char**, already discussed in the other SO question
return 0;
}
Possible output for char** :
&a = 3209288 // &a
&a[0] = 4083720 // &(*(a+0)) = a
&a[0][0] = 4083784 // &(*(*(a+0)+0)) = *a
a = 4083720 // a
a[0] = 4083784 // *(a+0) = *a
Possible output for 2D char array :
&a = 3473104 // &a
&a[0] = 3473104 // a
&a[0][0] = 3473104 // *a
a = 3473104 // a
a[0] = 3473104 // *a
It is easy to understand the output of char**. But the output of 2D char array looks strange though it was discussed in the other SO question. I cannot think of a pointer x of whatever data-type when,
x = &x = *x
and all the 3 things physically reside in the same block of memory. Hope that my confusion is understandable. Can anyone explain the mystery?