2

I know that in a bidimensional static array arr, arr is the same with arr[0], &arr[0], &arr[0][0], but I can't find why this also happens with *arr

  • See my answer here: https://stackoverflow.com/questions/33358758/issue-implementing-dynamic-array-of-structures/33358829#33358829 It has an extended explanation of how to access arrays vs pointers in all forms. – Craig Estey Dec 10 '18 at 22:25
  • `*arr` and `arr[0]` are defined as meaning the same thing – M.M Dec 10 '18 at 22:36

1 Answers1

3

Lets take a look at a simple two "dimensional" array (array of arrays) and how it looks like in memory.

First the array:

int a[2][2];

And how it looks like in memory (with some pointers):

+---------+---------+---------+---------+
| a[0][0] | a[0][1] | a[1][0] | a[1][1] |
+---------+---------+---------+---------+
^
|
&a
|
&a[0]
|
&a[0][0]

Looking at this it should be easy to understand why the pointers are the same.

To continue don't forget that all arrays can decay to a pointer to its first element. That is, for the array a above, then a is equal to &a[0]. That means *a is equal to *&a[0], which since the * and & operators cancel each other out, will be equal to a[0]. And since a[0] is an array it will decay to a pointer to its first element which is equal to &a[0][0]. That is, *a is equal to &a[0][0].

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