Multiple pointers to the same address
What you'll learn: How different pointer variables can all point to the same memory location, allowing multiple "names" to access and modify the same data.
The Office Desk Analogy
Imagine a specific office desk in a large building. This desk has a physical location (its address). Now, imagine two different employees are given a sticky note. Employee A's note says "The desk at Room 402." Employee B's note also says "The desk at Room 402."
Even though there are two separate sticky notes (two different pointer variables), they both lead to the exact same physical desk. If Employee A leaves a coffee cup on that desk, Employee B will see that coffee cup when they arrive, because they are looking at the same spot.
Connecting Pointers in Code
In C, you can assign the address of a single variable to as many pointers as you like. You simply set the value of one pointer to equal the value of another. Since a pointer’s "value" is just a memory address, they both end up holding the same number.
#include <stdio.h>
int main() {
int secretCode = 123;
// Both pointers are initialized to the address of secretCode
int *pointerA = &secretCode;
int *pointerB = pointerA;
printf("Value via pointerA: %d\n", *pointerA); // 123
printf("Value via pointerB: %d\n", *pointerB); // 123
// Changing the value through pointerA
*pointerA = 999;
// pointerB reflects the change because the underlying memory changed
printf("New value via pointerB: %d\n", *pointerB); // 999
return 0;
}
Why this matters
When you set pointerB = pointerA, you aren't making a copy of the data (secretCode). You are making a copy of the map to the data.
This is incredibly useful in C. You might have one pointer that stays at the start of a list, while another pointer "walks" through the list to find a specific item. Both pointers are looking at the same memory, but they serve different roles in your program. Just remember: if you use any one of these pointers to change the value at that address, every other pointer looking at that address will see the update immediately.
Key takeaway
Multiple pointers can hold the same memory address, acting as different aliases that provide access to the same single piece of data.