Question

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?

Was it helpful?

Solution

When you use name of the array is any expression except &array and sizeof array this name will be automaticly converted to pointer to the first element of the array (char [5][10] will be converted to char (*)[10]). Address of this pointer will be equal to the adress of entire array.
So, "sizeof (char [5][10]) == 50", there is no additional pointers.

char arr[5][10];

&a = 3473104 // Address of entire array, (char (*)[5][10])
&a[0] = 3473104 // Address of first row, (char (*)[10])
&a[0][0] = 3473104 // Address of first char, (char *)
a = 3473104 // "a", (char [5][10]), converted to "&a[0]", (char (*)[10])
a[0] = 3473104 // "a[0]", (char[10]), converted ro "&a[0][0]", (char *)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top