As I read here: http://www.cplusplus.com/reference/stl/array/operator%5B%5D/
it appears that saying a[2] would return the memory address (a reference) of the second element of a.
So how is
a[2]=5
a valid assignment, as that would mean I change the memory address of a[2] to location 5 (that might be possible, but usually you want to change the value, not the address) . Unless the = operator knows how to deal with this situation.
I know that it doesn’t change the memory address, so what’s actually going on here?
The difference between a reference and a pointer is that a reference is dereferenced automagically. Hence you don’t need such things as
*(a[2]) = 5.The following code shows this:
Changing either of
sameVaror*pBaseVarwill changebaseVaritself. ChangingpBasevaritself will not affectbasevar, it will simply cause to former to point to a different location.Under the covers (though this is, of course, implementation dependent,
basevaris probably considered (by the compiler/code) to be theintat a specific address (let’s say0x12345678),sameVaris considered that, too.pBaseVaris considered a pointer at (for example)0x11112222which happens to contain the value0x12345678: