Understanding Pointers in C, Finally

Pointers are just addresses. Once you see memory as a row of numbered boxes, the star and ampersand stop being magic — including why pointer bugs are the root of so many security holes.

· 3 min read · intermediate

Pointers confuse people because the syntax is introduced before the idea. The idea is simple: a pointer is a variable that holds a memory address. That's the whole thing.

Memory is numbered boxes

Picture RAM as a long row of boxes, each with an address and a value:

text
address:  1000   1004   1008   1012
value:  [  42 ] [  0  ] [ ... ] [ ... ]
            ▲
            x lives here

If int x = 42; put 42 at address 1000, then a pointer to x is simply a variable whose value is 1000.

The two operators

  • & means "address of" — give me where this lives.
  • * means "value at" — give me what lives at this address (dereferencing).
c
int x = 42;
int *p = &x;    // p holds the address of x  (e.g. 1000)

printf("%d\n", x);    // 42  — the value
printf("%p\n", &x);   // 1000 — the address of x
printf("%p\n", p);    // 1000 — same address, stored in p
printf("%d\n", *p);   // 42  — follow p back to the value

Read int *p as "*p is an int" — i.e. when you dereference p, you get an int. That reading makes declarations click.

Why pass a pointer?

C passes arguments by value — the function gets a copy. So this can't work:

c
void broken(int n)  { n = 99; }      /* changes the copy, not the caller's */

Pass the address instead, and the function can reach back and modify the original:

c
void works(int *n) { *n = 99; }      /* writes through the pointer */

int a = 1;
works(&a);
/* a is now 99 */

This is exactly how scanf("%d", &value) works — you hand it the address so it can fill your variable.

Pointers and arrays

An array name decays to a pointer to its first element, which is why these are equivalent:

c
int arr[3] = {10, 20, 30};
arr[1];       /* 20 */
*(arr + 1);   /* 20 — same thing; "+1" advances by one int, not one byte */

Pointer arithmetic is type-aware: arr + 1 moves sizeof(int) bytes, not one. The compiler scales for you.

Where pointers bite — and why security people care

Most infamous C vulnerabilities are pointer mistakes:

  • Null dereference — following a pointer that is NULL. Crash.
  • Dangling pointer / use-after-free — using a pointer after the memory it names has been freed. The memory may now hold something else entirely.
  • Buffer overflow — writing past the end of an allocation, trampling adjacent memory. For decades this was the way attackers overwrote return addresses and hijacked control flow.
  • Double free — freeing the same block twice, corrupting the allocator's bookkeeping.
c
int *p = malloc(sizeof(int));
free(p);
*p = 5;        /* use-after-free: undefined behaviour, a real exploit primitive */
p = NULL;      /* defensive habit: null it after freeing */

Habits that keep you sane

  • Initialise pointers; never leave them pointing at garbage.
  • Set a pointer to NULL after free, and check for NULL before dereferencing.
  • Match every malloc with exactly one free.
  • Build with -Wall -Wextra and run under a sanitiser (-fsanitize=address) — it catches use-after-free and overflows at runtime.

Takeaways

  • A pointer is just a variable holding an address; & takes an address, * follows one.
  • Pass pointers to let functions modify the caller's data (C is pass-by-value otherwise).
  • Pointer arithmetic is scaled by the pointed-to type.
  • The hardest bugs — and many classic exploits — are pointer lifetime mistakes; sanitizers and disciplined habits are your defence.

Related

Programming beginner

Automating Boring Tasks with Python

A practical starter for scripting the tedious stuff — files, APIs and small tools — with the error handling that makes a script trustworthy.

· 2 min read