Pointers vs. Arrays
Arrays and pointers have a lot in common. When we do:
int nums[10];
Then we get a spot in memory that looks like this:
But what is nums? It is actually a constant pointer to the first spot in the array, nums[0]
. So really, the picture looks like this:
So, &nums[0]
(the address of the first element in the array) is the same thing as nums
.
Pointer Notation
Because pointers and arrays are essentially the same thing (except array addresses cannot be
changed), we can also access elements in an array by treating it as a pointer. In the above
example, nums
is a pointer to the first spot in the array. Space for arrays is reserved
contiguously, so the second element in the array is physically next to the first element. This
means that I can say:
nums+1
to get the memory address of the second element in the array.
Note: an integer uses 4 bytes of space. However, you donβt say nums+4
to move to the next
integer. This is because pointers have a particular type associated with them β like an int β and
the compiler will automatically move over the space of an int when you say +1
.
Suppose now that you want to initialize the value at index 4 in the nums
array to 7. You could
say:
nums[4] = 7;
However, you could do the same thing by treating nums as a pointer. You can get the address of the array element at index 4 by saying:
nums+4
This is a pointer, so if we want to change the contents of that location to 7, we need to dereference it:
*(nums+4) = 7;
Example
Recall that an array is a constant pointer to a block of reserved memory. This means that we can change the values stored in the array, but we canβt change the array itself (make it reference another piece of memory). Consider the following statements β which are legal?
int a[10]; //OK β a points to a block of 10 ints in memory
a++; //NO β The address of an array canβt change
int *xp = a; //OK β Now xp also points to the beginning of the array
a = xp; //NO β The address of an array canβt change
int b[5]; //OK β b points to a block of 5 ints in memory
int *bp = b; //OK β Now b also points to the beginning of the array
xp++; //OK β Now xp points to the second element in the array
*xp = 14; //OK β The second element in the array is set to 14 (a[1] = 14)