[Solved] C++ references and pointers int∗ p = &a [ 2 ] ;


This code

int∗ p = &a[2];

is equivalent to this:

int∗ p = a + 2;

so even mathematically you can see that p - a is equal to a + 2 - a so I think result should be obvious that it is equal to 2.

Note name of array can decay to a pointer to the first element but is not that pointer, as many may mistakenly say so. There is significant difference.

About your note, yes there is difference, first expression assigns address of third element to p, second is syntax error as you try to assign int to int * which are different types:

int a[] = { 1, 2, 3 };
int var = a[2]; // type of a[2] is int and value is 3
int *pointer = &a[2]; // type of &a[2] is int * and value is address of element that holds value 3

Subject of your question says that you think & means reference in this context. It only means reference when applied to types, when used in expression it means different things:

int i, j;
int &r = i; // refence when applied to types
&i; // address of in this context
i&j; // binary AND in this

0

solved C++ references and pointers int∗ p = &a [ 2 ] ;