TL;DR
Every value a program uses lives in some numbered place in memory, and that number is its address. A pointer is a variable whose value is such an address, so instead of holding the data itself it says where the data is. Following the pointer to the data is called dereferencing. Pointers let two parts of a program share one value, and they let a program link pieces of data together in chains whose length changes as it runs.
Builds on: Binary Numbers (Bits and Bytes) for how an address is stored as a number, and Array for the idea that memory has positions · Leads to: Linked List (a chain of nodes joined by pointers), binary trees, and the rules for freeing memory in Garbage Collection
Where it's used
Linked structures. Most of the data structures in a typical program are built from pointers. A Linked List node holds a value and the address of the next node. A binary tree node holds a value and the addresses of its two children. A hash table often keeps each bucket's chain of entries as pointers. The shape of these structures can change at any moment, because adding a node only means writing one new address.
Operating systems. When a program asks for memory, the operating system does not hand over the memory itself. It hands back an address, and every later access goes through that address. The same idea appears one level down, where the processor's own instructions carry addresses that point at the next instruction or at data, and where Virtual Memory maps the addresses a program sees onto physical memory.
Shared state in a graphical app. A text editor might keep one document object, and several windows each hold a pointer to it. When the user types, the document changes once, and every window that follows the pointer sees the new text. Copying the whole document into each window would give stale copies that disagree.
How it works
Think of memory as a long row of numbered boxes. Each box holds one value and has an address, which is just its number. To keep the example small, this toy machine uses boxes that each hold one whole number, and a pointer is also stored in one box.
Suppose box 100 is an integer variable x and box 104 is a pointer variable p. Start with both empty.
-
Store a value in
x. Write 42 into box 100. Nowxholds 42. Check: box 100 holds 42, and nothing else has changed. -
Store the address of
xinp. The address ofxis 100, so write 100 into box 104. Nowpholds 100. It does not hold 42. Check: box 104 holds 100, which is a location, not the data. -
Dereference
p. Read whatpholds, which is 100. Go to box 100 and read what is there, which is 42. The pointer led the program to the value. Check:ppoints to box 100, and box 100 holds 42.
flowchart LR P["Read p from box 104: it holds 100"] --> G["Go to box 100"] G --> V["Read the value there: 42"]
-
Change the value through the pointer. Go to the box that
pnames, box 100, and write 7 there. Nowxholds 7, becausexand the boxpnames are the same box. Check: readingxdirectly now gives 7, and so does dereferencingp. -
Move the pointer. Suppose box 108 is another integer
ythat holds 5. Write 108 into box 104. Nowppoints aty, andxstill holds 7. Check: changingpchanged where it leads, but not the boxes themselves.
The key steps are 2 and 3. A pointer stores a location, and using it means one extra jump. The program reads the pointer, then reads the place it names.
The math
A pointer is a variable whose value is an address . If the address names a stored value , then refers to . Reading the value through the pointer is written and gives .
The formal version mirrors the steps above. Write for the address of a variable .
Step 2 in symbols: storing an address
The assignment copies the number 100 into . The value of is a number like any other, and it is stored in a box of its own, here box 104.
Step 3 in symbols: dereferencing
This is two reads, one of the pointer and one of its target. Every dereference costs at least one extra memory access compared with using a variable directly.
Step 4 in symbols: writing through a pointer
Writing through the pointer changes the box that names, which is why changes too.
How big is an address?
An address is a number, and its size limits how much memory a program can reach. A 32-bit pointer can name different bytes, about 4 gigabytes. A 64-bit pointer can name bytes, about , which is far more memory than any machine holds today. On a 64-bit machine a pointer takes 8 bytes, which is why the toy machine above, where addresses take 4 bytes, is only a model.
Pointer arithmetic: moving through an array
A pointer can carry that address and move by whole elements. If holds the address of element , then names element , and the address moves by bytes. In symbols, . This is why an array and a pointer into it are so closely related in languages like C.
Worked examples
Box 100 holds the integer x, which is 3. Box 104 holds the pointer p, which is 100.
- Dereference
p: go to box 100 and read 3. So*pis 3. - Compute
*p = *p + 1. The right side reads 3 and adds 1, giving 4. - Write 4 into the box that
pnames, which is box 100. Soxis now 4. - The pointer itself is unchanged: box 104 still holds 100.
Interpretation: the write went to box 100, because the pointer's value was 100. Only the target changed. A pointer's own value changes only when you assign to the pointer.
Let the array a be [7, 2, 9, 4, 6] with base address 200, and let each element take 4 bytes. Set to the address of element 0, which is 200.
- Element 0 is at . Dereferencing gives 7.
- Compute . This is 2 elements further on, so the address is .
- Dereference : box 208 holds 9. So
*(p + 2)is 9, which isa[2]. - To visit every element, add one element at a time: is at 204 and holds 2, is at 208 and holds 9, is at 212 and holds 4.
- The pointer advances by 4 bytes each step because each element is 4 bytes. The program never computes a position from scratch, it just moves .
Interpretation: a pointer into an array is the same address arithmetic as the Array lesson, with the index replaced by a step along the pointer.
A linked list of one million integers stores each node as a 4-byte value and an 8-byte pointer to the next node, on a 64-bit machine.
- Each node holds bytes of data.
- For one million nodes the data is bytes.
- In binary units that is mebibytes.
- The actual size is larger, because the machine pads nodes and its memory allocator keeps bookkeeping data for each block, but the pointers are the part that makes the structure grow in place.
- To find the 500th node, the program starts at the head and follows 499 pointers in turn. Each follow is a separate memory access, which is slower than reading an array index.
Interpretation: the list costs extra space for pointers and extra time to walk, in exchange for inserting and removing nodes without moving the others.
Try it: A linked list has 2,000 nodes, each 12 bytes of data as above. How many bytes of data does it hold?
Answer: bytes.
Common mistakes and tradeoffs
It is tempting to think that a pointer is the value it points to. In fact a pointer holds an address, a number that says where the value is. Two pointers can lead to the same value, and changing the value is visible through both, because both lead to the same box.
It is tempting to think that a pointer and an array are the same thing. In fact an array is a block of values in consecutive boxes, and a pointer is a box holding an address. In C, an array name is often converted to a pointer to its first element, which hides the difference. The difference matters when you assign, copy, or ask for the length.
It is tempting to think that a pointer is always safe to use. In fact there are two common ways a pointer goes wrong. A null pointer holds a special address value that means "points to nothing", and dereferencing it crashes or reads garbage. A dangling pointer still holds the address of memory that has been freed and may have been reused for something else, so reading through it gives unrelated data, and writing through it corrupts that data.
It is tempting to think that pointers are only for experts. In fact the same few rules cover everything: store an address, follow it to read or write, and be sure the target still exists. Most bugs come from breaking the third rule.
When to use a pointer: you need to share one value between several parts of a program, or you need a structure whose size and shape change while it runs. When not to use one: the data is small and copying it is cheap, or a plain array indexed by position does the job. Pointer chasing also hurts speed, because nodes scattered through memory are slow to reach, as the Memory Hierarchy lesson explains. Alternatives: storing indices into an array instead of addresses, which avoids dangling addresses and keeps data together; or a garbage-collected language, where the runtime decides when a value is no longer reachable, so the program does not free anything by hand.
History
Early computers already named memory by number, so a program could refer to a location by its address. Pointers as a language feature came as high-level languages were built on top of that hardware. In 1965, Tony Hoare added a null reference to the ALGOL W language. He later described it as a costly mistake, because it has caused so many program crashes since.
The C language, developed at Bell Labs in the early 1970s, made pointers central. It let programmers do address arithmetic directly, which is why the same ideas still appear in systems code today. Its design made the trade plain: fast and close to the machine, but the programmer must keep every pointer valid.
Pointers caught on because they match the machine. A processor reads memory by address, so a language that exposes addresses gives the programmer direct control over that work. The cost, bugs from invalid addresses, drove later languages to add checks, managed references, and automatic memory reclamation.
Recap
- Memory is a row of numbered boxes, and each value has an address.
- A pointer is a variable whose value is an address, not the data itself.
- Dereferencing a pointer reads the target, which costs one extra memory access.
- Pointer arithmetic moves by whole elements, so a pointer into an array uses the same address formula as an index.
- Pointers allow sharing and changing structures, at the cost of null and dangling pointer errors.
Read next: Linked List for a structure built entirely from pointers, binary tree for pointers that branch, and Garbage Collection for how managed languages free memory without the programmer tracking every pointer. Virtual Memory shows how the addresses a program sees map onto real memory.
Check yourself
QuestionWhat does a pointer store: the data it points to, or the address of the data?Show answer
It stores the address. The address says where the data is, and dereferencing the pointer is the step that reads the data at that address.
QuestionBox 300 holds the integer x, which is 11. Box 304 holds the pointer p, which is 300. What is x after the statement `*p = *p * 2`, and what does p hold?Show answer
Dereferencing p reads 11 from box 300, and is written back to box 300. So x is 22. The pointer p still holds 300, because the statement changed only the box p names.
QuestionWhy does dereferencing a null pointer cause trouble, even though the pointer is just a number?Show answer
The number zero, or the special null value, does not name a real value. Following it means reading or writing a box that holds no data the program meant to use, so the program crashes or reads garbage. Checking a pointer for null before following it prevents this.
QuestionA 64-bit program has an array of 1,000 values, each 8 bytes, starting at address 4000. Let q be a pointer to element 0. What address does q + 10 hold?Show answer
Each step moves by one element of 8 bytes, so q + 10 moves bytes. The address is .
Sources
- "Pointer (computer programming)", Wikipedia: https://en.wikipedia.org/wiki/Pointer_(computer_programming)
- "Null pointer", Wikipedia: https://en.wikipedia.org/wiki/Null_pointer
- "Memory management", Wikipedia: https://en.wikipedia.org/wiki/Memory_management