A pointer in C is a variable that stores the memory address of another object. You declare one with an asterisk, as in int *p;, and you set it with the address-of operator &, as in p = &x;. Reading or writing through the pointer uses the dereference operator *. This guide explains pointers in C from the ground up: what an address is, how to declare, initialise and dereference a pointer, and the mistakes that cause crashes.
Every variable lives somewhere in memory, and every byte of memory has an address (a number). A pointer holds one of those addresses. Instead of storing the value 10, a pointer to an int stores the location where an int is kept.
Pointers let a function change its caller's variables, let you work with arrays and strings without copying them, and let you allocate memory at run time. Almost every non-trivial C program depends on them.
The declaration has the form type *name;. The type says what kind of object the pointer points to:
The asterisk binds to the name, not the type. In int *a, b;, a is a pointer but b is a plain int. Write one declaration per line to avoid confusion.
The & operator gives the address of a variable. The * operator, applied to a pointer, accesses the object it points to:
The address printed will differ on your machine. Use %p with a cast to void * when printing pointers. Because p points at x, changing *p changes x.
An uninitialised pointer holds garbage, and dereferencing it is undefined behaviour. Always give a pointer a valid address, or set it to NULL when it points to nothing:
NULL is a null pointer constant from <stddef.h> (also available through <stdio.h> and <stdlib.h>). In C23 you can write nullptr instead. Dereferencing a null pointer is also undefined behaviour and usually ends in a crash, so check before you use a pointer you did not create yourself.
C passes arguments by value, so a function gets a copy of each argument. To let a function modify a variable, pass its address:
The function receives copies of the two addresses, but both copies point at the original variables, so the swap is visible to the caller.
| Mistake | Result | Fix |
|---|---|---|
| Using a pointer before assigning it | Undefined behaviour | Initialise at declaration |
Dereferencing NULL | Crash (segmentation fault) | Check for NULL first |
| Returning the address of a local variable | Dangling pointer, the variable is gone | Return a value, or use heap memory |
Mixing types: int *p = &some_double; | Wrong data read, constraint violation | Match the pointer type to the object |
Confusing *p and p | Reading the address instead of the value | Remember *p is the value, p is the address |
Use const when a function should only read through a pointer, for example void print(const int *p). The compiler then rejects any write through p.
In a declaration it means "this is a pointer". In an expression it means "the object this pointer points to". The same symbol has two roles depending on where it appears.
It depends on the platform. On a typical 64-bit system a data pointer is 8 bytes, whatever type it points to. Use sizeof(p) to check, and never assume it equals sizeof(int).
A void * can hold the address of any object type but cannot be dereferenced directly. You convert it back to a typed pointer first. Functions like malloc return void *.
A pointer to memory that is no longer valid, such as a freed block or a local variable of a function that has returned. Using it is undefined behaviour.
A pointer stores an address. Declare it with type *name, take addresses with &, access the target with *, and initialise every pointer, using NULL or nullptr when it points at nothing. Pass pointers to functions when they must modify the caller's data.