Pointer Basics
A pointer is a variable holding a memory address. The idea fits in one sentence, but its consequences are where things get interesting.
Declaring Pointers
The * in a declaration means "pointer to":
int x = 42;int *p = &x; // p holds the address of x&xis the address-of operator. Read it as "give me the address wherexis parked."*pis the dereference operator. It walks to the address inpand grabs whatever's there.
printf("Address of x: %p\n", (void *)p);printf("Value at that address: %d\n", *p);Pointers and Memory
Write int x = 42; and the compiler tucks x at some stack address. Pretend that's 0x7fff1234. Write int *p = &x; and the bytes inside p literally are 0x7fff1234.
Pointers are ordinary variables, so they have addresses of their own, and you can point at them too. Add int **pp = &p; and you get a chain of three stack slots (addresses made up, as before):
Each arrow is one dereference. *pp reads the box p and gets 0x7fff1234; **pp follows one more arrow and gets 42. x takes 4 bytes, while p and pp take 8 each on a 64-bit machine, whatever they point at.
A pointer doesn't "hold" the value it points at. It holds an address. To touch the actual data you dereference, walking that one extra hop. The whole indirection thing is exactly why pointers are so useful and so easy to misuse.
Two Declaration Traps
The * belongs to the variable name, not the type. This line declares one pointer and one plain int:
int *a, b; // a is int *, b is intint *c, *d; // two pointers: each name needs its own *The second trap is a pointer you declared but never set. A local int *p; holds whatever bytes were already on the stack, so *p = 5; writes to some arbitrary address. It might crash, or it might quietly overwrite another variable and fail much later. Give every pointer a real address or NULL the moment you declare it.
NULL Pointers
A pointer that aims at nothing in particular should be set to NULL:
int *p = NULL;
if (p != NULL) { printf("%d\n", *p);}Deref a NULL pointer and you've stepped into undefined behaviour territory. On most modern systems that means an immediate segfault, courtesy of the kernel keeping page zero unmapped. If you're not sure a pointer is good, check it first.
Pointers as Function Parameters
C is strictly pass-by-value. Want a function to mutate a variable from outside? Hand it the address:
void increment(int *n) { (*n)++;}
int main() { int x = 10; increment(&x); printf("%d\n", x); // prints 11}Drop the pointer and increment would happily bump a local copy, leaving the caller's x stuck at 10.
Tracing increment(&x)
Follow that call with made-up but realistic numbers. Say x lives at 0x7ffc0010 in main's stack frame:
| Step | What runs | n holds | x holds |
|---|---|---|---|
| 1 | int x = 10; | (no n yet) | 10 |
| 2 | increment(&x) copies the address into n | 0x7ffc0010 | 10 |
| 3 | (*n)++ reads 4 bytes at 0x7ffc0010 | 0x7ffc0010 | 10 |
| 4 | adds 1 and writes 4 bytes back to that address | 0x7ffc0010 | 11 |
| 5 | increment returns and n disappears | (gone) | 11 |
Pass-by-value still happened in step 2: n is a copy. It's a copy of the address rather than of 10, and both copies of an address lead to the same 4 bytes, so the write in step 4 lands in main's variable. The pointer's type is what tells *n to read 4 bytes and treat them as an int; a char * holding the same address would read one byte.
Predict: someone tidies the body of increment to *n++; with no brackets. What does main print now?
- Postfix
++binds tighter than*, so*n++means*(n++). It moves the local pointernon to the nextint's address, dereferences the old address, and throws the value away. Nothing ever writes tox. Compile with-Walland both GCC and Clang warn that the result is unused, which is one of many reasons to keep warnings on.
Here's the classic test of whether that's sunk in. swap_values takes its arguments by value, swap_pointers takes addresses. Predict both output lines before you run it.
You can't fix swap_values, and that's the point: it only ever sees copies.