HOWTO · C
How to Get Size of a Pointer in C
Learn how sizeof measures a pointer object in C and how that differs from the pointed-to object and an allocation.
On this page
To get the size of a pointer in C, apply the sizeof operator to the pointer expression, such as sizeof(pointer). This measures the pointer object itself, not the object at the address it stores and not the amount of memory requested from an allocator. The result is an implementation-dependent number of bytes; C does not promise one fixed pointer size for every system or pointer type. Use size_t and the %zu format specifier when displaying a sizeof result.
What a Pointer’s Size Means in C
A pointer is an object whose value designates another object or a function. A declaration such as int *integerPointer declares a pointer to int; assigning &value gives it the address of an existing int object. The pointer occupies storage of its own, and its size is what sizeof(integerPointer) reports. That size is determined by the C implementation and can depend on the target and its data model.
Do not infer the pointer size from the type it points to. A char *, int *, and double * need not have the same representation on every implementation, and the C language does not require all object pointers to have a universal byte count. Even when several pointer types happen to have equal sizes on your machine, treat that as a property of that implementation rather than a portable constant. sizeof gives the correct value for the current build.
Get a Pointer’s Size with sizeof
sizeof is an operator, not a function or method. For an object expression such as pointer, it produces a value of type size_t, the unsigned integer type used to represent object sizes. Include <stdio.h> to print the result and use %zu, which is the printf conversion specifier for size_t. Avoid %lu: size_t is not required to be unsigned long, so that format can mismatch the argument on some implementations.
Run this example:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int value = 42;
int *pointer = &value;
printf("Pointer object: %zu bytes\n", sizeof(pointer));
char *buffer = malloc(12 * sizeof *buffer);
if (buffer != NULL) {
printf("Pointer object: %zu bytes\n", sizeof(buffer));
printf("Requested allocation: %zu bytes\n", 12 * sizeof *buffer);
free(buffer);
}
return 0;
}
The first result is the size of pointer. The second is the size of buffer, which is also a pointer object. The requested allocation expression is a separate calculation: it asks malloc for storage for twelve char elements. malloc returns a pointer to that storage, but the pointer variable does not grow to match the allocation. The actual allocator may reserve additional bookkeeping space, which this expression does not measure.
Compare Pointer and Pointed-to Object Sizes
The * operator in an expression dereferences a pointer. Therefore sizeof(*pointer) measures the type of the pointed-to object, while sizeof(pointer) measures the pointer. For the int example, the former is the size of an int; it is not another way to ask for the pointer size. These values can differ, and their exact values depend on the implementation.
The distinction also applies when the pointer refers to a scalar, an array element, or dynamically allocated storage. For example, if characterPointer points to a char, then sizeof(characterPointer) measures the pointer and sizeof(*characterPointer) measures one char. If a pointer points at the first element of an array, sizeof(*pointer) still describes one element; it does not recover the full array length. Keep the pointer expression and the object expression explicit to avoid labeling one result as the other.
When you need a displayed size, store or pass the result as size_t and print it with %zu, as in printf("%zu\n", sizeof(pointer));. This formatting remains appropriate even when the implementation uses a different underlying unsigned integer type for size_t.
Check a Dynamically Allocated Pointer
Dynamic allocation does not change what sizeof means. In char *cPointer = malloc(sizeof(char));, sizeof(char) determines the requested number of bytes for one character, while sizeof(cPointer) determines the size of the pointer variable. sizeof(*cPointer) measures one pointed-to char. These are three distinct quantities: requested storage, pointer object, and pointed-to object.
For a larger request, calculate the element count times the element size, commonly count * sizeof *cPointer. This form follows the pointed-to type if the declaration changes. Check that malloc returned a non-null pointer before using allocated storage, and release successful allocations with free. Do not use sizeof(cPointer) as the allocation length: it only describes the pointer object and may be much smaller than the requested data region. Likewise, do not report sizeof(*cPointer) as the pointer size. Use sizeof(cPointer) for that task, and use the allocation expression when you need to describe how many bytes the program requested.