The last lesson ended on a problem: C passes arguments by value, so a function only ever gets copies and cannot reach back and change the variables you handed it. Pointers are how we get around that — and once you understand them, memory, strings, arrays, and every challenge in this section stop being mysterious.
A pointer is a simple idea wrapped in intimidating syntax. Think of your computer's memory as one enormous row of numbered boxes, each holding one byte. Those numbers are addresses. Every variable you declare lives in some of those boxes, at some address. A pointer is just a variable that holds an address instead of an ordinary value — it tells you where something is rather than what it is.
Two operators are the whole game. The & operator means address of: &x gives you the address where x lives. The * operator, when you already have a pointer, means the value at that address — we call this dereferencing.
To declare a pointer, you write the type it points to, a *, and a name:
1int x = 42;
2int *p = &x; // p holds the address of xNow p holds the address of x, and *p reaches through that address to get 42. Here is the same thing as a picture — one box holding a value, and another box holding the first box's address:
These addresses are not hand-waving — they are real numbers you can look at. Let's prove it with a tiny program and a debugger. Here is the program:
1#include <stdio.h>
2
3int main(void)
4{
5 int x We compile it with -g so the debugger knows our variable names, then stop inside main with GDB and inspect x and p directly. If you have never seen GDB before, do not worry about every line — focus on what each command asks and what comes back:
gcc -g ptr.c -o ptrset output-radix 10break 8Breakpoint 1 at 0x1150: file ptr.c, line 8.runBreakpoint 1, main () at ptr.c:8p x$1 = 42p &x$2 = (int *) 0x7fffffffe1d4p p$3 = (int *) 0x7fffffffe1d4p *p$4 = 42Read it from the top. We first switch GDB to print numbers in plain decimal; then break 8 tells it to pause at line 8, and run starts the program, which stops just before the printf. Now p (short for print) asks GDB for a value:
p x is 42, the value we stored.p &x is the address of x — where it lives in memory.p p is the value held by our pointer, and it is the same address. That is the entire point: p is holding x's address.p *p dereferences the pointer — follows the address — and lands back on 42.The $1, $2 labels are just GDB numbering its answers; ignore them. What matters is that p and &x print the identical address, and *p reaches through it to the value. The exact addresses you see will differ every run and on every machine — it is the relationships between them that matter, not the specific numbers.
You might wonder why a pointer has a type at all. If p just holds an address — a plain number — why does C make us write int *p instead of one generic "pointer"?
Because the type answers a question the address alone cannot: when I dereference this, how many bytes should I read? An address only names the first byte. An int * knows to read 4 bytes from there; a char * reads just 1. The same address, dereferenced through two different pointer types, hands back different values.
1char *cp; // *cp reads 1 byte
2int *ip; // *ip reads 4 bytes
3long *lp; // *lp reads 8 bytesThe type also controls how pointer arithmetic steps through memory, which is the next section. There is one special case worth knowing now: void * is a typeless pointer. It can hold any address, but because it has no type, you cannot dereference it until you cast it to a real pointer type first. You will meet void * the moment we reach malloc.
Adding to a pointer does not add bytes — it adds elements. Because the pointer knows its type, p + 1 moves forward by exactly one sizeof the thing it points to. For an int *, p + 1 jumps ahead 4 bytes, landing neatly on the next integer.
This is what makes arrays and pointers so closely related. An array stores its elements back to back in memory, and the array's name acts as a pointer to its first element. So walking an array is just repeatedly stepping a pointer:
1#include <stdio.h>
2
3int main(void)
4{
5 int arrThe debugger makes the stepping concrete. Watch the addresses climb by 4, and watch *(p + 1) pull out the second element:
gcc -g arr.c -o arrset output-radix 10break 8Breakpoint 1 at 0x1165: file arr.c, line 8.runBreakpoint 1, main () at arr.c:8p arr$1 = {10, 20, 30, 40}p p$2 = (int *) 0x7fffffffe1c0p p + 1$3 = (int *) 0x7fffffffe1c4p *(p + 1)$4 = 20x/4dw arr0x7fffffffe1c0: 10 20 30 40p arr prints the whole array; p p shows our pointer sitting on the first element; and p p + 1 is that address plus 4, exactly one int further along. x/4dw arr is GDB's examine command — "show me 4 decimal words starting here" — and it lays the four integers out at their rising addresses. Stepping a pointer and indexing an array (arr[1]) are two ways of saying the same thing.
Here is the payoff promised at the start. Because a pointer holds an address, you can hand a function the address of one of your variables, and the function can reach back through that address to change the original — copies and all.
The classic example is swapping two values. This cannot work by value, because the function would only swap its own copies. With pointers, it works:
1void swap(int *a, int *b)
2{
3 int tmp = *a;You call it by passing addresses, using &:
1int x = 1, y = 2;
2swap(&x, &y); // x is now 2, y is now 1The function still receives copies — but they are copies of addresses, and a copy of an address points to the exact same box in memory as the original. When swap writes *a = *b, it is writing into the caller's x. This pattern, passing a pointer so a function can modify your data, is everywhere in C: it is how functions return more than one value, and how you avoid copying large structures around.
A pointer holds an address, but sometimes you need one that deliberately points at nothing — a placeholder for "no address yet." That value is NULL, which is address 0:
int *p = NULL;The rule is simple and strict: never dereference a pointer that does not point at valid memory. Address 0 is never valid, so reading or writing through a NULL pointer crashes the program immediately. It is worth seeing exactly what that crash looks like in the debugger, because you will cause it, and recognizing it saves hours:
1#include <stdio.h>
2
3int main(void)
4{
5 int gcc -g crash.c -o crashrunProgram received signal SIGSEGV, Segmentation fault.0x0000555555555139 in main () at crash.c:66 *p = 1; // write through a NULL pointerSIGSEGV — a segmentation fault — is the operating system stopping your program for touching memory it is not allowed to. When GDB shows you a SIGSEGV, a bad pointer is almost always the cause, and the line it points to is where the bad access happened. Here it is *p = 1, writing through NULL.
The fix is to check before you dereference:
1if (p != NULL)
2{
3 *p = 1;
4}A dangling pointer is the subtler cousin of NULL: a pointer that looks valid but points at memory that is no longer yours. A common way to create one is returning the address of a local variable:
1int *broken(void)
2{
3 int local = 5;
4 return &This is a question of scope — the stretch of a program where a variable exists. A variable declared inside a function is local to it: C creates it when the function is called and throws it away the instant the function returns. So local lives only while broken runs; the moment it returns, that memory is handed back and reused for something else, and the address you returned now points at garbage. We look at exactly where local variables live, and why they vanish, in The Stack; for now the rule is enough — never return the address of a local variable. The same trap appears when you keep using memory after freeing it — a use-after-free — which we will cover alongside malloc and free in Dynamic Memory.
That is the pointer. It is worth rereading until the picture — a box holding another box's address — feels obvious, because every lesson after this one leans on it. Next we put pointers to work on Dynamic Memory: asking the operating system for memory at runtime and taking responsibility for giving it back.