How many bytes do pointers take up?
Categories:
Understanding Pointer Sizes in C: A Deep Dive

Explore the factors determining the size of pointers in C, from architecture to compiler, and learn how to accurately determine pointer sizes.
Pointers are fundamental to C programming, enabling direct memory manipulation, dynamic memory allocation, and efficient data structures. A common question for newcomers and experienced developers alike is: "How many bytes does a pointer take up?" The answer, while seemingly simple, depends on several crucial factors. This article will demystify pointer sizes, explaining the underlying principles and providing practical ways to determine them.
The Basics: What is a Pointer?
At its core, a pointer is a variable that stores a memory address. Instead of holding a direct value, it holds the location in memory where another value is stored. This allows programs to indirectly access and modify data. Regardless of the data type it points to (e.g., int
, char
, float
, or even a custom struct
), a pointer itself stores an address. The size of this address is what determines the pointer's size.
flowchart TD A[Variable 'x' (int)] --> B[Memory Address 0x1000] C[Pointer 'ptr' (int*)] --> D[Stores 0x1000] B -- "Contains value 42" --> A D -- "Points to" --> B E[Size of 'ptr'] --> F["Determined by system architecture (e.g., 4 or 8 bytes)"]
Conceptual diagram of a pointer storing a memory address
Factors Influencing Pointer Size
The size of a pointer is not fixed across all systems or compilers. It is primarily determined by the architecture of the machine and the operating system it runs on. Here are the key factors:
void
can hold any object pointer, and a pointer to char
has a size of 1 byte for its dereferenced value, but not for the pointer itself.1. System Architecture (Word Size)
The most significant factor is the system's architecture, specifically its "word size" or the size of its memory addresses. Modern systems are typically either 32-bit or 64-bit:
- 32-bit systems: On a 32-bit system, memory addresses are 32 bits long. Since 1 byte equals 8 bits, a 32-bit address requires 4 bytes to store. Therefore, pointers on a 32-bit system are typically 4 bytes in size.
- 64-bit systems: On a 64-bit system, memory addresses are 64 bits long. This means a 64-bit address requires 8 bytes to store. Consequently, pointers on a 64-bit system are typically 8 bytes in size.
This is why you often see pointers being 4 bytes on older or embedded systems and 8 bytes on most modern desktop and server environments.
2. Compiler and Operating System
While the architecture sets the general expectation, the specific compiler and operating system can also play a role. Compilers are designed to generate code for a particular target architecture. They adhere to the Application Binary Interface (ABI) of the target system, which dictates conventions like pointer sizes. For instance, a 64-bit compiler running on a 64-bit OS will typically produce 8-byte pointers, even if the underlying CPU could theoretically support 32-bit addressing modes (though this is rare for general-purpose computing).
It's important to note that all data pointers (e.g., int*
, char*
, float*
, struct MyStruct*
) will have the same size on a given system, as they all store memory addresses. The only exception might be function pointers, which could theoretically have a different size on some exotic architectures, though this is uncommon in practice.
Determining Pointer Size Programmatically
The sizeof
operator in C is your best friend for determining the size of any type or variable at compile time. To find the size of a pointer, you can apply sizeof
to a pointer variable or a pointer type.
#include <stdio.h>
int main() {
int *ptr_int;
char *ptr_char;
double *ptr_double;
void *ptr_void;
printf("Size of int*:\t\t%zu bytes\n", sizeof(ptr_int));
printf("Size of char*:\t\t%zu bytes\n", sizeof(ptr_char));
printf("Size of double*:\t%zu bytes\n", sizeof(ptr_double));
printf("Size of void*:\t\t%zu bytes\n", sizeof(ptr_void));
printf("Size of a generic pointer:\t%zu bytes\n", sizeof(void*));
return 0;
}
C code to determine pointer sizes using sizeof
When you compile and run this code on a typical 64-bit system, the output will look something like this:
Size of int*: 8 bytes
Size of char*: 8 bytes
Size of double*: 8 bytes
Size of void*: 8 bytes
Size of a generic pointer: 8 bytes
Example output on a 64-bit system
On a 32-bit system, all these values would typically be 4 bytes. This demonstrates that the type of data the pointer points to does not affect the pointer's own size; only the address size matters.
sizeof
to determine the size of types and variables. Never hardcode pointer sizes (e.g., assuming 4 or 8 bytes), as this can lead to non-portable code and subtle bugs when migrating to different architectures.