Given an array variable such as int myArray[6]; what exactly does the variable myArray itself store (if we don’t add in the brackets to refer to a specific element)? It turns out, myArray holds the address of the zeroth element of the array! That is, myArray is a pointer of type int. So:
myArray == &myArray[0]
But it gets even better than that: The computer can do arithmetic, giving us the ability to easily find the address of myArray[1] or myArray[2], for example:
myArray + 1 == &myArray[1]
myArray + 2 == &myArray[2]
This is called pointer arithmetic. So by adding 1 to the address myArray, the computer automatically locates the address of the next array element, &myArray[1].
Watch the video to find out more.
If you cannot see this codecast, please click here.
Suppose you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
What is *(numbers+4)?
Feel free to use the window below to try out some code. Be sure to work the correct answer out "by hand" first before verifying your answer using the code window.