Pointers

A pointer is a box that holds the address of another box. Draw it as an arrow.

The idea

Imagine a friend’s house. A value is the house itself. A pointer is a note with the house’s street address written on it. If you hand someone a photocopy of the house (pass by value) they can paint it and your house stays the same. If you hand them the address note (a pointer) they can walk there and paint your real house.

Two symbols do all the work: &x means “the address of x” (make the arrow) and *p means “go where p points” (follow the arrow, to read or write).

Watch it work

Try it

Common mistakes