Pierre FG
Pierre FG

Reputation: 11

Array indirection issue

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]

Upvotes: 1

Views: 62

Answers (1)

Stephan Lechner
Stephan Lechner

Reputation: 35164

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.

Upvotes: 1

Related Questions