Section 19.9 Arrays and Pointers
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 .
Video Description.
The array variable itself holds the address of the zeroth element of the array
Adding "1" to the pointer of an array will update the address to the next element of the array
i.e. *array = 3
, *(array+1)=10
will update the first (the zeroth, truthfully speaking) number in the array as 3 and the next as 10
Check Your Understanding Check Your Understanding
1.
Let’s start with a review question!
What is the output of the following program:
#include <stdio.h>
void swap (char *x, char *y);
int main(void) {
char c = 'P';
char d = 'T';
printf("%c%c ", c, d);
swap(&c, &d);
printf("%c%c", c, d);
return 0;
}
void swap (char *x, char *y) {
char temp;
temp = *x;
*x = *y;
*y = temp;
}
Correct
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
2.
Suppose you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
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.
admin ..... open in new window
Correct
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
3.
Suppose you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
What is
*numbers+4
? Notice that the only difference to the previous question is the omission of parenthesis.
You really need to think about this first before just trying it out. Really! Anyway, here is the code window for you to try out some code.
admin ..... open in new window
Correct
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
4.
Suppose (again...) you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
Random and depends on what is at that memory location
Correct
Not quite. Try again!
This will cause a crash of your program
Not quite. Try again!
The compiler sets this to zero
Not quite. Try again!
Not quite. Try again!
5.
Suppose (for the last time) you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
You have furthermore declared a pointer via
int *ptr;
ptr = numbers + 2;
You want to try this out, right? Remember... Think first!
admin ..... open in new window
Correct
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!
Not quite. Try again!