cadence_learn
the_c++_track / level 14 of 15 ▶ 18 min

a box holding a box number.

The part everyone warns you about, and it is one idea: memory is numbered, and a pointer holds one of those numbers. Two symbols do all the work, and they are opposites.

after this level you'll be able to
  • Say what &x and *p each give you, and why they are opposites
  • Change a variable through a pointer without naming it
  • Explain a segmentation fault, and why it is the good failure
the one everyone warns you about

it is a box holding the number of another box

the one sentence

Memory is numbered boxes — you saw that in level 7. A pointer is just a box whose contents are the number of another box. That is the entire idea.

Pointers have a reputation, and it is mostly earned by the notation rather than the concept. Two symbols do all the work, and they are opposites:

int x = 42; int *p = &x; // p holds the ADDRESS of x cout << x; // 42 the value cout << &x; // 0x16db4a29c where it lives cout << p; // 0x16db4a29c the same address cout << *p; // 42 go there and look *p = 99; // change what is THERE cout << x; // 99 — x changed, without ever naming x

Every one of those was produced by a real run. & asks where something lives. * goes to an address and looks. That is it.

the value, the address, and going there
MEMORY — THE NUMBERED BOXES FROM LEVEL 7 0x16db4a294 0x16db4a29c 42 int x 0x16db4a2a4 0x16db4a2b0 0x16db4a29c int *p p HOLDS the address of x x the value in the box 42 &x the box’s address 0x16db4a29c p a box holding that address 0x16db4a29c *p go there and look 42
Two symbols, and they are opposites. & asks “where does this live?” and * says “go to that place and look”. Everything else about pointers is those two, repeated.
◈ ask an ai about this

“What is a pointer in C++? Explain & and * with a simple picture, assuming I know memory is numbered boxes.”

chatgpt ↗ claude ↗

try it yourself

follow the arrow

▤ lab 13 · the memory bench

Click an expression and watch which box lights up.

Start with x, then &x, then *p. Watch which box the answer comes from.
the confusing bit, said plainly

The * means two different things depending on where it is. In int *p it is part of the type — “p is a pointer to an int”. In *p = 99 it is an action — “go there”. Same symbol, two jobs. Nobody finds this obvious, and knowing that it is genuinely two things is most of the battle.

so why bother

you have already used one

Level 8’s int &x reference — the one that let a function change the caller’s variable — is a pointer with the sharp edges filed off. A reference is another name for an existing box. A pointer is a box that holds the address, which you can change, and which can point at nothing at all.

void addOne(int &x) { x = x + 1; } // reference — simpler, use this void addOne(int *p) { *p = *p + 1; } // pointer — same effect, more typing addOne(v); // reference version addOne(&v); // pointer version — you pass the address yourself

Prefer references when you have the choice. They cannot be null, they cannot be re-pointed, and they read better. Pointers matter because arrays are really pointers underneath, because older code is full of them, and because they are how one thing refers to another thing that may not exist yet.

the crash you will eventually meet

A pointer that points at nothing is nullptr — verified, it prints as 0. Following one with *p is the classic segmentation fault: the program is stopped by the operating system for reaching into memory that is not its own.

Unlike almost everything else in this course, this failure is loud — and that is a mercy. A pointer to the wrong place would be far worse: it would keep running and quietly corrupt something. Check for nullptr before following a pointer, every time.

◈ ask an ai about this

“What is the difference between a pointer and a reference in C++, and what causes a segmentation fault?”

chatgpt ↗ claude ↗

do this together

the cloakroom

☶ two people · 12 minutes · numbered slips of paper

Number six containers 1 to 6. Put a value in one. Then hand each other tickets.

person a

Put 42 in container 3. Write “3” on a slip and hand it over. The slip is a pointer; the number on it is an address.

person b

Asked for the slip, say “3”. Asked to follow it, go to container 3 and read out 42. Then change what is in container 3 without ever being told it was container 3.

Then two more: hand B a slip saying 0 and ask them to follow it — that is a null pointer, and the right answer is to refuse. Then hand them a slip saying 9 when there are only six containers. The second is far more dangerous than the first, and feeling why is the whole lesson.

what to keep

three things worth remembering

01

& is where, * is go there

Two opposite questions. Everything else about pointers is those two repeated.

02

Prefer a reference

Same power for the common case, no null, no re-pointing, and far easier to read.

03

A loud crash is the good case

Following null stops the program. Following a wrong address does not — it corrupts something quietly.

check yourself

3 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself3 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

What does this print?

int x = 42; int *p = &x; *p = 99; cout << x;
02

What is the difference between &x and *p?

03

You follow a pointer that is nullptr. What happens?

answered: 0 of 3right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →