Difference Between *(Pointer + Index) and Pointer[]
Functionally, they are identical.
Semantically, the pointer dereference says "Here's a thing, but I really care about the thing X
spaces over", while the array access says "Here's a bunch of things, I care about the Xth
one."
In most cases, I would prefer the array form.
No, they are functionally equivalent.
First, index
is scaled up to the type size then added to the myPointer
base, then the value is extracted from that memory location.
The "better practice" is the more readable one, which is usually, but not necessarily always, the myPointer[index]
variant.
That's because you're usually interested in an element of the array, not the memory location to dereference.
There is no difference between
*(array+10); //and
array[10];
but guess what? since +
is commutative
*(10 + array); //is all the same
10[array]; //! it's true try it !