11.2) Relationship Between Arrays And Pointers
An array variable is like a pointer that always points to a particular memory address, that of the first element in the array; it can't be incremented like a pointer variable, but we can use it in arithmetic expressions as a way of pointing to different array elements.
Consider the following example:
int x[5] = {1, 4, 9, 16, 25}; cout << x << endl; // prints address of first element cout << *x << endl; // prints 1 cout << *(x+1) << endl; // prints 4 *(x+2) = 0; // equivalent to x[2] = 0
x
is effectively a pointer to the first element, so*x
is equivalent tox[0]
x+1
is a pointer to the second element, so*(x+1)
is equivalent tox[1]
, etc- The size of the array is 5, so
x
points to the start of the array andx+5
points just past the end - which should sound familiar! (Remember iterators?)