[Beginner → Intermediate] — A variable holding an address. Simple to state, and the thing everything else in C is built from.
The basics
int x = 42;
int *p = &x; // p holds the ADDRESS of x
printf("%d\n", *p); // 42 — dereference: "the value at that address"
*p = 100; // writes through the pointer; x is now 100
printf("%p\n", (void *)p); // print an address — cast to void*, use %p&— address-of*— in a declaration, “is a pointer”; in an expression, “dereference”
Declaration syntax is the confusing part:
int *a, b; // a is int*, b is int ← the * binds to the NAME
int *a, *b; // both are pointersWhich is why int *p is the conventional spacing rather than int* p — it reflects what actually happens. C’s declaration syntax is “declaration mirrors use”, and it doesn’t age well.
Pointer arithmetic
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
p + 1 // advances by sizeof(int) bytes, not 1 byte
*(p + 2) // 30
p[2] // identical — a[i] IS *(a + i), by definitionArithmetic scales by the pointed-to type. p + 1 on an int* moves 4 bytes; on a char*, 1 byte; on a struct Foo*, sizeof(struct Foo).
Because a[i] is defined as *(a + i), and addition commutes:
arr[2] == *(arr + 2) == *(2 + arr) == 2[arr] // all legal. 2[arr] compiles.A curiosity, not advice.
ptrdiff_t n = p2 - p1; // elements between them, not bytesArithmetic is only defined within a single array (plus one past the end). Computing a pointer outside that is UB even if you never dereference it. The one-past-the-end pointer is legal to form and compare, illegal to dereference — which is what makes for (p = arr; p < arr + n; p++) valid.
void *
void *generic; // a pointer to unknown type
int *p = malloc(10 * sizeof *p); // malloc returns void*, converts implicitly in Cvoid * converts to and from any object pointer without a cast in C. It’s how malloc, memcpy and qsort are type-agnostic — and it’s a total hole in the type system, since nothing checks you got it right.
You cannot dereference or do arithmetic on void * — its size is unknown. (GCC allows void* arithmetic as an extension, treating it as char*; don’t rely on it.)
// Don't cast malloc's result in C. It's unnecessary, and it hides a missing
// #include <stdlib.h>, which used to cause real bugs.
int *p = malloc(n * sizeof *p); // good — note sizeof *p, not sizeof(int)sizeof *p rather than sizeof(int) means changing p’s type doesn’t silently break the allocation. Use it.
NULL
int *p = NULL;
if (p == NULL) { }
if (!p) { } // equivalent, and commonNULL is a null pointer constant. Dereferencing it is undefined behaviour — in practice a segfault on a hosted OS, because page zero is unmapped. On embedded targets with no MMU, it may silently read address 0 and continue with garbage, which is far worse.
free(p);
p = NULL; // defensive: prevents use-after-free and makes double-free a no-opfree(NULL) is explicitly a no-op, so setting to NULL after freeing is safe and worth doing.
Always check the result of anything that can return NULL:
FILE *f = fopen(path, "r");
if (!f) { perror("fopen"); return -1; }
char *buf = malloc(n);
if (!buf) { return -1; }The absence of Option<T> means nothing forces this. It’s the discipline C asks of you and the one people skip.
Pointers to pointers
int x = 5;
int *p = &x;
int **pp = &p;
**pp = 10; // x is now 10Two real uses:
Output parameters — modifying the caller’s pointer:
int alloc_buffer(char **out, size_t n) {
char *p = malloc(n);
if (!p) return -1;
*out = p; // write through, so the CALLER's pointer changes
return 0;
}
char *buf;
if (alloc_buffer(&buf, 128) != 0) { /* handle */ }Arrays of pointers — char **argv is the canonical example:
int main(int argc, char **argv) {
for (int i = 0; i < argc; i++) printf("%s\n", argv[i]);
}C is strictly pass-by-value. Passing a pointer lets a function modify what it points at; passing a pointer to a pointer lets it modify the pointer itself.
Function pointers
int add(int a, int b) { return a + b; }
int (*op)(int, int) = add; // declaration: name in the middle, parens mandatory
int r = op(2, 3); // call it
int r = (*op)(2, 3); // same thing; the older spellingThe parentheses around *op are required — without them, int *op(int, int) declares a function returning int*.
Almost always worth a typedef:
typedef int (*BinaryOp)(int, int);
BinaryOp op = add;Function pointers are C’s mechanism for polymorphism, callbacks, and dispatch tables:
// The standard library's sorting hook
int cmp(const void *a, const void *b) {
return (*(const int *)a) - (*(const int *)b); // note: this subtraction can overflow
}
qsort(arr, n, sizeof(int), cmp);// A vtable, by hand — this is what C++ virtual methods compile to
struct Shape {
double (*area)(const struct Shape *);
void (*draw)(const struct Shape *);
};That struct-of-function-pointers pattern is how you get interfaces in C, and it’s exactly what `dyn Trait` and a C++ vtable are underneath. Writing it by hand once makes dynamic dispatch permanently unmysterious.
const with pointers
const char *p; // pointer to const char — can't write *p, CAN reassign p
char *const p; // const pointer to char — can write *p, can't reassign p
const char *const p; // neitherRead right to left. const char *const p = “p is a const pointer to a const char”.
The first form is the one you write constantly — a function that reads but doesn’t modify:
size_t my_strlen(const char *s);
void print_all(const int *arr, size_t n);Taking const is free documentation, lets callers pass literals, and occasionally helps the optimiser.
Why pointers are dangerous
Everything C’s reputation rests on. All of these compile without warning:
int *p; // uninitialised — points at garbage
*p = 5; // UB
int *p = malloc(4);
free(p);
*p = 5; // use-after-free
int *dangling(void) {
int x = 5;
return &x; // returns a pointer to a dead stack frame
}
int arr[5];
arr[10] = 1; // out of bounds; no check exists
free(p); free(p); // double free — heap corruptionThere is no runtime check for any of this. The pointer doesn’t know what it points at, how big it is, whether it’s still valid, or whether anyone else holds it.
This is the entire motivation for Rust’s ownership system:
| C failure | What Rust does |
|---|---|
| use-after-free | ownership — you can’t use a moved value |
| dangling pointer | lifetimes — a reference can’t outlive its owner |
| double free | one owner, one drop |
| buffer overrun | bounds-checked slices |
| null dereference | no null; Option<T> |
| data race | the borrow rule |
The tools that catch these in C — ASan, Valgrind — are runtime and only find bugs on paths you actually execute. → Debugging and Tooling
Practical rules
- Initialise every pointer. To
NULLif nothing else. - Set to
NULLafterfree. - Check every allocation and every
fopen. conston any pointer parameter you don’t modify.- Never return a pointer to a local.
- Pass the length alongside the pointer. A bare pointer carries no size, and every buffer overflow starts there.
sizeof *poversizeof(Type)in allocations.
Related
- Arrays, Strings and Decay — where pointers and arrays blur
- Memory Management — the heap, and every way to lose it
- Undefined Behaviour — the consequences
- Rust: Ownership — the systematic fix
- C course map
Software engineering
Infrastructure & security
Computer science
Hardware & engineering