Alex Rivera | Logout

Memory map for a 2D array in C

Asked 2011-07-13T03:23:37.580
13

Do you think what this discussion about memory-map of 2D array is correct? Especially this photo? Can you explain the theory?

Suppose we declare a 2D array in C like this:

int arr[3][3]={10, 20, 30, 40, 50, 60, 70, 80, 90};

Now, according to this discussion, the memory would be arranged like the following:

enter image description here

Now, I have written the following code to test this theory:

#include <stdio.h>

main()
{
    int arr[3][3]={10, 20, 30, 40, 50, 60, 70, 80, 90};
    printf("      arr==%d\n", arr);
    printf("  &arr[0]==%d\n", &arr[0]);
    printf("   arr[0]==%d\n", arr[0]);
    printf("&arr[0][0]=%d\n", &arr[0][0]);
    printf(" arr[0][0]=%d\n", arr[0][0]);
}
/*
Output:
========
      arr ==1245028
  &arr[0] ==1245028
   arr[0] ==1245028
&arr[0][0]==1245028
 arr[0][0]==10
Press any key to continue...
*/

Why the first 4 outputs are same?

Edit
Report

1 Answer

0

The first four outputs are the same why of obtaining the address of the first element in the array, i.e. &arr[0][0]. The last output is its content.

answered 2011-07-13T03:28:58.210

Your Answer