- Published on
Understanding C Pointers and Double Pointers Through a Classroom Metaphor
- Authors

- Name
- Mike Schmitt
Pointers in C can be hard to grasp at first. Let’s make them easier by thinking about them in terms of a classroom, a teacher, and a stack of notecards.
✨ Part 1: The Classroom Story (No Code Yet)
Imagine you’re a student in a classroom.
- At the front of the room is a big bookshelf where you and other students can keep your study materials.
- You also have a stack of notecards on your desk for quick notes.
- The teacher manages who can use the bookshelf and how much space they get.
- And sometimes you call in a friend to help with tasks.
📒 Your Notecards
- Each notecard is a quick place to jot something down.
- You can write information directly on the card, or just write down a shelf number where something is stored on the bookshelf.
- When class ends, you throw away all your notecards.
🗄 The Bookshelf
- The bookshelf is big and shared by the students in this classroom, but not automatically with people in other classrooms.
- You can’t just walk up and use it — the teacher insists that you ask first.
- If the teacher finds space, they give you a shelf number written on a notecard.
- If not, they say “Sorry, no room.”
- When you’re finished with a shelf, you must tell the teacher so the space can be used again.
📖 Books
- The books are your actual study materials.
- Small books might fit directly on a notecard.
- Larger ones belong on the bookshelf, with the notecard pointing to their location.
🧑 Friends
Sometimes you call in a friend to help:
- If you hand them a notecard with a shelf number, they can go to the bookshelf and rearrange the books for you.
- But if they ask the teacher for a new shelf, the teacher gives them a notecard.
- Your friend doesn’t know what to do with it, so when they leave, they throw it away.
📘 Solving the Problem
To fix this:
- Instead of just letting your friend get their own notecard, you give them one of your own blank notecards.
- When the teacher hands them a shelf number, they write it directly on your card.
- Now, when they leave, you still have the updated shelf number.
🔍 Part 2: Mapping the Story to C Pointers
Now let’s connect the metaphor to C programming concepts.
📒 Stack: Automatic Variables (Your Notecards)
- For this metaphor, the stack is a quick, function-local workspace.
- Each notecard represents a local variable with automatic storage duration.
- For ordinary block-scope automatic variables, lifetime runs from entry to exit of the associated block. C does not require them to occupy a physical stack.
void study() {
int x = 42; // a value written directly on a notecard
} // x disappears when the function ends
🗄 Heap: Dynamically Allocated Memory (The Bookshelf)
- Here, the bookshelf represents dynamically allocated memory in one process.
- Threads in that process can access the same allocations; other processes do not automatically share them.
- The C allocator manages requests made with
mallocand returns a memory address (a shelf number) when it succeeds.
#include <stdlib.h>
int main(void) {
int *p = malloc(sizeof(int)); // teacher gives you a shelf
if (p != NULL) {
*p = 42; // put a book on the shelf
}
free(p); // tell the teacher you're done
return 0;
}
⚠️ If you don’t free(p), the allocation remains in use until it is freed or the process exits (a memory leak if it is no longer needed).
The allocator is the teacher in this part of the analogy; a call to malloc does not mean that the program directly asks the operating system for one shelf.
For another view of how address spaces and per-thread stacks fit together, see the restaurant-kitchen model of processes and threads.
🖊 Pointers (The Notecards with Shelf Numbers)
- A pointer stores an address; in this metaphor, it is a notecard with a shelf number written on it.
- You can follow the number to access the book.
#include <stdio.h>
int main(void) {
int book = 10; // book written directly on a card
int *card = &book; // card points to the book
printf("%d\n", *card); // use the card to read the book
return 0;
}
🧑 Functions and Pointers
- If you give your friend (a function) just the shelf number, they can read or change the book.
- But if they ask for a new shelf, their notecard disappears when they leave.
- In the following code, the function receives a pointer, but assigning a new address to its parameter does not update the caller's pointer.
C passes arguments by value, so p is a copy of the caller's pointer. A valid incoming pointer could still point to a useful object; the problem is that assigning the result of malloc changes only this local copy.
#include <stdlib.h>
void allocateWrong(int *p) {
p = malloc(sizeof(int)); // teacher gives friend a shelf
if (p == NULL) {
return;
}
*p = 99; // friend writes a book
} // friend's notecard is thrown away
If the allocation succeeds, its address is lost when the function returns, so the caller cannot free it. When you return to your desk, you don’t know where the book is.
📘 Double Pointers (Letting Your Friend Update Your Card)
One solution is to give your friend one of your own notecards so they can update it for you.
#include <stdio.h>
#include <stdlib.h>
void allocateRight(int **p) {
*p = malloc(sizeof(int)); // teacher gives friend a shelf
if (*p != NULL) {
**p = 99; // put the book on the shelf
}
}
int main(void) {
int *card = NULL;
allocateRight(&card); // give friend your notecard
if (card != NULL) {
printf("%d\n", *card); // your notecard now has the shelf number
}
free(card); // clean up when done
return 0;
}
⚠️ This is the essence of a double pointer:
- A single pointer stores an address; in the metaphor, it is a shelf number.
- A double pointer is the address of the card itself.
- We pass the address of the pointer so the function can update it to refer to the allocated memory.
🎯 Key Lessons
- Automatic local variables (notecards): ordinary block-scope variables live for their associated block; scope and lifetime are distinct, and physical placement is an implementation detail.
- Dynamically allocated memory (bookshelf): managed by an allocator; the process's threads can share it, and the program should free allocations it no longer needs.
- Pointer (card): just a shelf number, not the book itself.
- Double pointer: a way to give functions access to your card, so they can update it directly.
✅ By first imagining a classroom with notecards, a bookshelf, and a teacher, the behavior of C pointers and double pointers becomes much easier to understand.
Get new articles by email
After confirming your address, you’ll receive an email whenever I publish a new blog article. Unsubscribe anytime.