The idea: a pointer is a variable that holds an address
Memory is a long row of bytes, and every byte has a number: its address. A variable
takes a few bytes in a row. An int takes 4, so int x = 42; might sit in the
bytes 0x1000 to 0x1003. A pointer is a variable whose value is
one of those addresses. That is all it is: a number that says where something else lives.
The canvas shows one small program on a toy 64-bit machine: an int is 4 bytes and a
pointer is 8. Each cell has its address written next to it. The blue cells on the left are the pointer
variables p and pp; the number inside is what they hold. Pick a statement and
press Run. A grey box carries each value, one hop per step. A blue arrow is a pointer you may
use; a red arrow is one you must not.
& and *: take an address, follow an address
&x is the address of x. Here it is 0x1000.
After p = &x; the cell of p holds 0x1000.
*p goes the other way. It reads the address in p, goes to that cell, and
stands for the variable that lives there. This is called dereferencing. So
cout << *p; prints 42, and *p = 99; changes x to 99 even
though the statement never says x (Demo 1).
The * in a declaration means something else: int *p says "p is a
pointer to int". The type matters, because it tells the compiler how many bytes to read at the address
and how far one step is.
p + 1 steps by the size of the type
p + 1 does not add 1 to the address. It moves to the next element, so it adds
sizeof(*p) bytes. For an int* that is 4: 0x1004 + 1 gives
0x1008 (Demo 2).
| Pointer type | sizeof element | p + 1 adds | p + 3 adds |
|---|---|---|---|
char* | 1 | 1 byte | 3 bytes |
int* | 4 | 4 bytes | 12 bytes |
double* | 8 | 8 bytes | 24 bytes |
Arithmetic only has a meaning inside one array. A pointer may also sit one past the
end of the array. Loops use that address as their stop mark. You may hold it and compare it,
but you may not use *p there, and you may not step further (Demo 6: after four
steps p holds 0x1014, which here is padding, not an element).
An array name used as a pointer
An array is a block of elements side by side: a[0] at 0x1004,
a[1] at 0x1008, and so on. When you use the name a as a value, it
turns into a pointer to its first element. So p = a; is the same as
p = &a[0];. People say the array decays to a pointer.
Indexing is pointer arithmetic with a nicer spelling: a[i] means
*(a + i). In Demo 2, p = a then p + 1 reaches
a[1] and prints 20; two more steps and *p = 99 writes a[3].
The array is still not a pointer. sizeof(a) is 16 (four ints), while
sizeof(p) is 8. And you cannot assign to a or write a + 1 back
into it. A function that takes int *p gets only the address, so it cannot know the length;
pass the length too.
Pointer to pointer
p is a variable, so it has an address too: 0x1018 on the canvas.
int **pp = &p; stores that address. Now *pp is p, and
**pp is whatever p points at. Two stars, two hops.
In Demo 3, **pp = 77 first lands in x. Then p is
re-aimed at the array, and the very same statement lands in a[0]. pp did not
change; it follows whatever p holds now. This is how a function changes a caller's pointer:
it takes an int** (or, in C++, an int*&).
Null and dangling pointers
A pointer is only useful while its address belongs to a living object. There are three common ways for that to be false.
| Pointer | What p holds | *p |
|---|---|---|
| ✓ valid | the address of a live object | that object |
| ✗ null | 0 (nullptr) | crash: segmentation fault on most systems (Demo 4) |
| ✗ dangling | an address whose object is gone | undefined behaviour: may seem to work (Demo 5) |
| ✗ one past the end | the address right after the last element | undefined behaviour (Demo 6) |
A null pointer is the honest "points at nothing" value. You can test for it:
if (p). delete on it does nothing.
A dangling pointer is worse. In Demo 5, delete p; frees the
int at 0x2000, but p still holds 0x2000. Nothing in p
says the memory is gone. The usual habit is to write p = nullptr; right after
delete, so a later mistake crashes at once instead of quietly corrupting data. The same
thing happens when a function returns the address of one of its local variables.
The opposite mistake is a leak: the pointer moves on while the heap int is still allocated, and now nobody knows its address to delete it (Demo 7).
In modern C++, std::unique_ptr, std::vector and references remove most of
these traps. Raw pointers are still what all of them are built from.
See also Stack vs Heap in C for where these cells really live (stack frames, malloc chunks, and what a use-after-free does to the allocator), Pass by Value vs Pass by Reference for what a call copies, Malloc Allocators for how the heap hands out addresses, Endianness for the order of the 4 bytes inside one int, and C++ Compile & Link.
See also Parameter passing modes for what a by-reference parameter holds in the callee's frame compared with a copy or a thunk.
See also Struct padding and alignment in C for why p + 1 on a struct pointer steps by a sizeof that includes padding.
What the page leaves out
Real addresses are 48-bit numbers like 0x7ffd3c2a1b40 and change on every run. The compiler
picks the order of local variables, so x, a and p need not be
neighbours. The page also leaves out p - q and pointer comparison, const
pointers, void* and casts, pointers to structs (p->field), function
pointers, references, new[] / delete[], smart pointers, uninitialised
("wild") pointers, and the strict aliasing rule. A real program stops at the first crash; the page
skips the bad statement so you can keep trying.