An array is like a constant pointer, it's always pointing at the first element of the the array. To try to point it at something else is an error.
array[2]
is short for
*(array + 2*sizeof(<type of array>))
which takes the address of the first element and adds the appropriate number of bytes to it in order to access the requested element, then it dereferences the "pointer" and you get the value of the element.
As RegEx implies, pointer arithmetic implicitly includes the sizeof(<array type>) term, i.e. given
int* array;
then
array + 1
points to the next int, not the next byte. And so array[2] == *(array + 2). (The fact that addition commutes means that using 2[array] instead is valid and works in C.)
Additionally, (as you know, but merely pointing out for the curious), `sizeof arr` returns the size of arr in bytes, not the size of a pointer to the first element of arr.
Does this compile? I thought an array was treated like a constant pointer, inexistent in memory so you cannot take its address, increment it, or attribute it another value. Although the point made by RegEx about sizeof, which I didn't remember, convinced me that an array is not actually a constant pointer.
To make my thoughts clear, if arr is equivalent to &arr[0], then wouldn't &arr be equivalent to &&arr[0]?
An array is like a constant pointer, it's always pointing at the first element of the the array. To try to point it at something else is an error.
is short for which takes the address of the first element and adds the appropriate number of bytes to it in order to access the requested element, then it dereferences the "pointer" and you get the value of the element.If this isn't 100% correct, please let me know.