Skip to main content
Logo image

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.

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;
    }
    
  • PT TP
  • Correct
  • PT xy
  • Not quite. Try again!
  • PT PT
  • Not quite. Try again!
  • PT yx
  • Not quite. Try again!
  • None of the above
  • Not quite. Try again!

2.

    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.

    admin.....open in new window

  • 0
  • Correct
  • -1
  • Not quite. Try again!
  • 7
  • Not quite. Try again!
  • -11
  • Not quite. Try again!
  • 10
  • Not quite. Try again!
  • 5
  • Not quite. Try again!
  • None of the above
  • 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

  • 9
  • Correct
  • 5
  • Not quite. Try again!
  • 7
  • Not quite. Try again!
  • 4
  • Not quite. Try again!
  • 0
  • Not quite. Try again!
  • -1
  • Not quite. Try again!

4.

    Suppose (again...) you have declared an array of integers by typing
    int numbers[] = {5, -1, 7, 10, 0, -11};
    
    What is numbers[10]?
  • Random and depends on what is at that memory location
  • Correct
  • 5
  • 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!
  • -11
  • 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;
    
    and initialized it via
    ptr = numbers + 2;
    
    What is *(++ptr)?
    You want to try this out, right? Remember... Think first!

    admin.....open in new window

  • 10
  • Correct
  • -11
  • Not quite. Try again!
  • 5
  • Not quite. Try again!
  • 0
  • Not quite. Try again!
  • -1
  • Not quite. Try again!
  • 7
  • Not quite. Try again!