The size of a pointer variable
What you'll learn: Why all pointers on your computer take up the same amount of memory, regardless of what they point to.
The Mailbox Analogy
Imagine you are looking at a row of houses. One house is a tiny studio apartment (a char), another is a large family home (an int), and another is a massive mansion (a double). These buildings are all different sizes.
However, if you write down their addresses on a sticky note, every sticky note is exactly the same size. It doesn't take a bigger piece of paper to write the address of a mansion than it does to write the address of a shed. The address is just a number that tells you where to find the building.
In C, a pointer is that sticky note. It is a variable that stores a memory address.
One size fits all
Whether a pointer points to a single character (1 byte) or a giant array (1,000 bytes), the pointer variable itself remains a fixed size. This is because the pointer only needs to hold the "starting location" of the data in your RAM.
The size of a pointer depends on your computer's architecture, not the data type:
- On a 32-bit system, every pointer is typically 4 bytes.
- On a 64-bit system, every pointer is typically 8 bytes.
Seeing it in code
We can use the sizeof operator to prove this. Even though a double is much larger than a char, their "address notes" are identical in size.
#include <stdio.h>
int main() {
char c = 'A';
double d = 3.14;
// Create pointers to these variables
char *charPtr = &c;
double *doublePtr = &d;
// Check the size of the pointers, not the data
printf("Size of char pointer: %zu bytes\n", sizeof(charPtr));
printf("Size of double pointer: %zu bytes\n", sizeof(doublePtr));
return 0;
}
If you run this on a modern 64-bit computer, both will print 8 bytes. Even though a double is eight times larger than a char, the map to find them is the same size.
Why this matters
Knowing that pointers have a consistent size allows the computer to manage memory efficiently. It knows exactly how much space to set aside for an address before it even knows what kind of data is sitting at that address.
Key takeaway
A pointer’s size is determined by your CPU's architecture, not by the data type it points to.