Systems CourseC/C++ · Linux · Memory
C++ CoreLesson 02 of 441 min read7 questions

Pointers

A variable that holds a memory address.

Test yourself ↓

Concept core idea Watch out easy to get wrong

Concept

Key concepts

  1. A pointer holds an address. * dereferences (read/write the pointed-to value); & takes an address.
  2. The pointer variable itself lives on the stack; what it points to may be stack or heap.
  3. A pointer can be reseated (change which address it holds) and can be null.

The two operators

&x = 'address of x'. *p = 'the value at the address p holds'. Inverses: *(&x) is x.

C++
int x=42; int* p=&x;
*p = 99;   // writes through p -> x is 99
Watch out

Easily confused

* vs &
In a declaration int* p, * means 'pointer'. In an expression, *p dereferences and &x takes an address.
p vs &p
p = the address it points to (e.g. a heap block). &p = where the pointer itself lives (on the stack).
null vs dangling
A null pointer points to nothing (0). A dangling pointer points to freed/out-of-scope memory — using it is UB.

Test yourself

7 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.