Lecture 9: Pointers

Intro to pointers

Declaring and using pointers video

Using pointers to pass by reference video

Arrays as pointers video

Segementation fault video

Optional reading: chapter 6

Key ideas:

  • What is a pointer?
  • Declarations and manipulation of pointers
  • & operator: address of
  • * symbol meanings:
    1. In a variable declaration statement, * means “pointer to”
    2. In executable statements, * is the indirection operator and means “follow the pointer”
    3. * is also a binary operator (needing two inputs) when we use it to mean multiplication.
  • Pass by reference vs. pass by value review
  • Equivalence of arrays and pointers
  • Pointer arithmetic for arrays
  • Why do we use pointers? To pass values by reference, to access arrays effectively, to allocate memory dynamically, and to define complex data structures.