Pointers and References
A value that is a memory address. Pointers let code share, modify, and link data without copying it.
Level: intermediate
What is it?
A pointer is a variable whose value is a memory address. The address-of operator (&) produces one and the dereference operator (*) follows it. Pointers let a function modify a caller's data, build linked structures, and refer to large objects without copying them. Languages without raw pointers still have references, which are pointers the runtime keeps safe.
Why does it matter?
Pointers explain arrays, strings, linked lists, trees, shared mutable state, and most memory bugs. Even in Python or Java, 'is this a copy or the same object?' is a pointer question.
Mental model
A street address on an envelope. The address is not the house: you can copy the address a thousand times and still have one house. Follow the address (dereference) to reach the house; hand out a wrong or stale address and the mail goes nowhere.
How it works
- &x gives the address of x; *p reads or writes the value at address p.
- Passing a pointer lets the callee change the caller's variable; passing by value gives it a copy.
- An array name usually decays to a pointer to its first element, and p + 1 advances by one element, not one byte.
- NULL means 'points nowhere'. Dereferencing it crashes; check before you follow.
- Common defects: null dereference, dangling pointer, out-of-bounds arithmetic, and double free.
- References in Java, Python, and JavaScript are pointers with guard rails: no arithmetic, and the collector prevents dangling.
Steps
- Take an address with &
- Store it in a pointer
- Pass the pointer around
- Dereference with *
- Stop using it after the target dies
Code example: Swapping through pointers
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int main(void) {
int x = 1, y = 2;
swap(&x, &y);
printf("%d %d\n", x, y); /* 2 1 */
return 0;
}Real-world usage
- Linked lists, trees, and graphs
- Passing large structs without copying them
- Systems code: buffers, memory-mapped I/O, and system call arguments
Common mistakes
- Dereferencing a pointer that is NULL or uninitialized.
- Keeping a pointer to freed memory or to a returned frame.
- Confusing the pointer with the thing it points to when comparing or copying.
Interview questions
What is the difference between a pointer and a reference?
A pointer is an address you can reassign, do arithmetic on, and set to null. A reference is an alias the language binds to an existing object and usually cannot be null or rebound. Managed languages' references add safety on top of the same idea.
What is a null pointer dereference?
Reading or writing through a pointer that holds no valid address. In most systems it terminates the program; in memory-safe languages it raises an exception such as NullPointerException.
How does pointer arithmetic work?
Adding n to a pointer to type T advances it by n × sizeof(T) bytes. That is why p + 1 steps to the next element of an array regardless of element size.