C++ Core
Pointers
A variable that holds a memory address.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
- A pointer holds an address.
*dereferences (read/write the pointed-to value);&takes an address. - The pointer variable itself lives on the stack; what it points to may be stack or heap.
- 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.
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.