Open In App

Size of Pointers in C

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

As pointers in C store the memory addresses, their size is independent of the type of data they are pointing to. This size of pointers in C only depends on the system architecture. That is why it is equal for every pointer type for same operating system and CPU architecture.

The pointer size reflects the maximum addressable memory space of the system. For example,

  • 8 bytes for a 64-bit System
  • 4 bytes for a 32-bit System

The reason for the same size is that the pointers store the memory addresses, no matter what type they are. As the space required to store the addresses of the different memory locations in the given architecture is the same, the memory required by one pointer type will be equal to the memory required by other pointer types.

How to Find the Size of Pointers in C?

We can find the size of pointers using the sizeof operator as shown in the following program:

C
#include <stdio.h>

struct str {
};

void f(int a, int b){};

int main() {
    int a = 10;
    char c = 'G';
    struct str s;

    // Pointers to different types
    int* ptr_int = &a;
    char* ptr_char = &c;
    struct str* ptr_str = &s;
    void (*ptr_func)(int, int) = &f;
    void* ptr_vn = NULL;

    // Printing sizes
    printf("Size of Integer Pointer  \t:\t%lu bytes\n",
           sizeof(ptr_int));
    printf("Size of Character Pointer\t:\t%lu bytes\n",
           sizeof(ptr_char));
    printf("Size of Structure Pointer\t:\t%lu bytes\n",
           sizeof(ptr_str));
    printf("Size of Function Pointer\t:\t%lu bytes\n",
           sizeof(ptr_func));
    printf("Size of NULL Void Pointer\t:\t%lu bytes",
           sizeof(ptr_vn));

    return 0;
}

Output
Size of Integer Pointer  	:	8 bytes
Size of Character Pointer	:	8 bytes
Size of Structure Pointer	:	8 bytes
Size of Function Pointer	:	8 bytes
Size of NULL Void Pointer	:	8 bytes

As we can see, no matter what the type of pointer it is, the size of each and every pointer is the same.

Now, one may wonder that if the size of all the pointers is the same, then why do we need to declare the pointer type in the declaration? The type declaration is needed in the pointer for dereferencing and pointer arithmetic purposes.

How Pointer Size Helps in Pointer Arithmetic?

Assume two pointers p1 and p2 where p1 points to an integer and p2 points to a character. The size of the pointer is same but the size of the data it is pointing to is different (4 bytes and 1 bytes). Dereferencing these pointers allows us to read the data pointed by these pointers. But how does the compiler know how many bytes of data read just by using pointer name?

Yes, you guessed it right! The compiler determines how many bytes of data is to be read from the type declaration of a pointer. That is also why the void pointers cannot be dereferenced (as void type does not have any defined size).

Same things happen with other pointer arithmetic operations.


Next Article
Article Tags :

Similar Reads