-4

I have confusion about 2d arrays in C. My code is:

main()
{
    int i, j;
    int arr[3][4] = {
                     { 1, 2, 3, 4 },
                     { 5, 6, 7, 8 },
                     { 9, 0, 1, 6 }
                    };    

    printf("\narr: %u", arr);
    printf("\n&arr: %u", &arr);
    printf("\n*arr: %u", *arr);
}

And the Output of the above program is:

arr u: 3215469448
&arr: 3215469448
*arr: 3215469448

Now tell me how all 3 printf statements displaying address of a single location. Just explain broadly how these addresses are same.

I know, similar question is asked earlier also, but they didn't help me. Don't refer me to Memory map for a 2D array in C and MEMORY MAP for 2d / multidimensional arrays ...

Community
  • 1
  • 1
foo
  • 1
  • 1
  • 2
    *"Don't refer me to Memory map for a 2D array in C and MEMORY MAP for 2d / multidimensional arrays ..."* Why not? You have asked the same question and the answer remains the same. An array decays to a pointer and the address of the array is also that pointer and a prefix-`*` dereference on a multidimensional array returns the first lower dimensional array which decays to a pointer... โ€“ dmckee --- ex-moderator kitten Nov 30 '12 at 05:22
  • probably typo in `arr u: 3215469448`. `u` not needed. I tried to fix that, but occur error `too short fix`. โ€“ mattn Nov 30 '12 at 05:32
  • You need to do your own research across the thousands of resources on this exact same question including the two links you have provided. Voting to close "please explain this code to me" as too localized, although yes it is also arguable this is an exact duplicate to the questions that you say did not help you. โ€“ djechlin Nov 30 '12 at 05:33

2 Answers2

1

In memory, your array is laid out like this

Low address                                  High address

  +---+---+---+---+---+---+---+---+---+---+---+---+
  | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 1 | 6 |
  +---+---+---+---+---+---+---+---+---+---+---+---+
  ^               ^               ^
  |               |               |
arr[0]          arr[1]          arr[2]
  ^
  |
 arr

As seen in the above crude ASCII-image, both arr and arr[0] points to the same memory location. And as *arr is the same as arr[0] (which is the same as arr) then it's easy to see why arr and *arr are the same.

The that might seem weird is that &arr is the same too, conventionally it should be the location of the variable arr not what it points to. The reason can be found in the C specification (ยง6.5.3.2/3 in the last C11 draft):

Similarly, if the operand is the result of a [] operator, neither the & operator nor the unary * that is implied by the [] is evaluated and the result is as if the & operator were removed and the [] operator were changed to a + operator.

What this means is, basically, that if the address-of operator & is used on an array, just return the address of the array.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

At the first, arr point to top address of the 2d array memory block. this mean that address of variable arr is same as address of memory block of 2d array.

printf("\narr: %u", arr);

show the pointer value

printf("\n&arr: %u", &arr);

show an reference of variable arr. It's same as top address of 2d block in I said above.

printf("\n&arr: %u", *arr);

arr is a pointer that is pointed address of a[0] also, so *arr mean dereference of &arr[0]. i.e. *&arr[0] = arr.

mattn
  • 7,571
  • 30
  • 54