Pointers Explained
Addresses & Pointers · Memory & Systems track
3 min read
In this module
A Variable That Holds an Address
A pointer is just a variable whose value is a memory address. Where a normal variable stores 42 or 'A', a pointer stores the address of where some other piece of data is sitting. That indirection — one variable telling you where to find another — is one of the sharpest tools in the low-level toolbox.
Declaring and Using Pointers in C
In C, you declare a pointer by adding an asterisk to the type:
int x = 42; // a normal integer, stored at some addressint *p = &x; // a pointer to int, storing the ADDRESS of xThe & operator hands back the address of a variable. The * operator, used on a pointer, dereferences it — follows the address and fetches whatever's parked there:
printf("%d", *p); // prints 42 - the value at the address p holdsPick some addresses to make it concrete. Say x lives at 0x1000 and p at 0x1008 (the exact numbers are illustrative; they'll be different on every run). Both are ordinary chunks of memory. The only thing special about p is that the number inside it happens to be x's address:
& goes from a variable to its address. * goes from an address back to whatever's stored there. They're opposites, so *&x is just x.
A pointer stores the memory address of another value. The & (address-of) operator gets a variable's address. The * (dereference) operator follows a pointer to access the data at that address.
Why Pointers Are Powerful
A few things become possible (or at least practical) only because pointers exist:
- Passing by reference — copy a big struct into every function call and you pay the cost every time. Pass a pointer instead and the function reads or writes the original data directly.
- Dynamic data structures — linked lists, trees, graphs all hang together with pointers connecting nodes scattered around heap memory.
- Arrays — in C, an array name used in an expression turns into ("decays to") a pointer to its first element, so array indexing is pointer arithmetic dressed up in syntactic sugar. The array itself isn't a pointer, though:
sizeofon an array gives the whole array's size, not a pointer's. - Hardware interaction — device drivers and embedded code use pointers to read and write specific memory-mapped hardware registers.
The first point is the one you'll hit soonest. C passes arguments by value, so a function receives copies. Run this, see why swap_copies changes nothing, then compare the addresses each version works with.
Pointer Size
A pointer's size hinges on the system's address width, not on what it points to. On a 64-bit system, every pointer is 8 bytes (64 bits) whether it's pointing to a single char, an int, or a 4 KB struct. The pointer just has to be big enough to hold any address the system can produce.
Pointers as a Mental Model
Picture a pointer as a slip of paper with an apartment number scrawled on it. The paper isn't the apartment. It tells you where one is. You can photocopy the paper (copy the pointer), scribble out the number and write a new one (reassign), or actually walk to the apartment and look inside (dereference). The data and the address of the data are separate things, and pointers let you handle both.