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.
What is the size of Pointer variable in C ?
Pointer is the variable that stores the address of another variable.
How to calculate size of Pointer ?
Consider the following memory map before declaring the variable.In the memory comprise of blocks of 1 byte.
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.
Calculating Size of Pointer : Tips
- Pointer stores the address of the Variable.
- Size of Pointer Variable can be evaluated by using sizeof operator
- Address of variable is considered as integer value.
- For Borland C/C++ Compiler, for storing integer value 2 bytes are required.
- Thus Pointer variable requires 2 bytes of memory.
Size of Integer Pointer Variable 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 the Pointer :
#include<stdio.h> int main() { int num = 15; int *ptr = NULL; ptr = # printf("Size of Pointer : %d Bytes",sizeof(ptr)); return 0; }
Output :
Size of Pointer : 2 Bytes