Skip to main content
Be Bitwise
CPointers Deep Dive

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":

c
int x = 42;
int *p = &x; // p holds the address of x
  • &x is the address-of operator. Read it as "give me the address where x is parked."
  • *p is the dereference operator. It walks to the address in p and grabs whatever's there.
c
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.

Key Concept

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:

c
int *a, b; // a is int *, b is int
int *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:

c
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:

c
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:

StepWhat runsn holdsx holds
1int x = 10;(no n yet)10
2increment(&x) copies the address into n0x7ffc001010
3(*n)++ reads 4 bytes at 0x7ffc00100x7ffc001010
4adds 1 and writes 4 bytes back to that address0x7ffc001011
5increment 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?
  1. Postfix ++ binds tighter than *, so *n++ means *(n++). It moves the local pointer n on to the next int's address, dereferences the old address, and throws the value away. Nothing ever writes to x. Compile with -Wall and 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.

Try itPredict both lines, run it, then try to make swap_values work without changing its parameter types.
C (GCC 9.2.0)

You can't fix swap_values, and that's the point: it only ever sees copies.