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.
&x and *p each give you, and why they are oppositesMemory 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:
Every one of those was produced by a real run. & asks where something lives. * goes to an address and looks. That is it.
& asks “where does this live?” and * says “go to that place and look”. Everything else about pointers is those two, repeated.Click an expression and watch which box lights up.
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.
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.
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.
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.
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.
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.
Two opposite questions. Everything else about pointers is those two repeated.
Same power for the common case, no null, no re-pointing, and far easier to read.
Following null stops the program. Following a wrong address does not — it corrupts something quietly.
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.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
What does this print?
What is the difference between &x and *p?
You follow a pointer that is nullptr. What happens?
← back to the whole C++ track · stuck on anything? ask peter.