C size of pointer variable



In the previous chapter, we have learnt about the memory organization of the pointer variable. In this chapter we are learning to compute or find the size of pointer variable.

Size of Pointer variable in C

Pointer is the variable that stores the address of another variable.

Size calculation

Consider the following memory map before declaring the variable.In the memory comprise of blocks of 1 byte.

Step 1 : Memory Before Variable Declaration

Whenever we declare any variable then random block of memory is chosen and value will be stored at that memory location.

Similarly whenever we declare any pointer variable then random block of memory will be used as pointer variable which can hold the address of another variable.

Program

#include<stdio.h>

int main()
{
int *iptr = NULL;
float *fptr = NULL;
char *cptr = NULL;

printf("\nSize of Integer Pointer   : %d Bytes",sizeof(iptr));
printf("\nSize of Character Pointer : %d Bytes",sizeof(cptr));
printf("\nSize of Float Pointer     : %d Bytes",sizeof(fptr));

return 0;
}

Output :

Size of Integer Pointer   : 4 Bytes
Size of Character Pointer : 4 Bytes
Size of Float Pointer     : 4 Bytes

Explanation

  1. Pointer stores the address of the Variable.
  2. Size of Pointer Variable can be evaluated by using sizeof operator
  3. Address of variable is considered as integer value.
  4. For Borland C/C++ Compiler, for storing integer value 2 bytes are required.
  5. Thus Pointer variable requires 2 bytes of memory.

Size of pointer in different IDE

Compiler Size of Integer Size of Integer Pointer
Borland C/C++ 2 Bytes 2 Bytes
Turbo C/C++ 2 Bytes 2 Bytes
Visual C++ 4 Bytes 4 bytes

Size of pointer : Example Programs