1

I have an issue with array indirections in C. Let's declare an array :

int tab[3];

How can these three variable display the same result ? It looks like the memory cell of the tab contains the adress itself AND the first value. I don't understand.

tab
&tab
&tab[0]
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Pierre FG
  • 11
  • 2

1 Answers1

1

Arrays like int tab[3] decay to a pointer to the first element when tab is used in a context where a pointer is expected. &tab[0] is - obviously - the address of the first element, too.

Similar but a bit different is &tab, since this is actually a pointer of type int (*)[3], i.e. it points to an array of ints of size 3. The address is still the same as that of the first element in tab; yet you may observe differences incrementing such a pointer, and you may get warnings when assigning like int* x = &tab.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58