The values of pointer variables are unsigned integer numbers which are addresses of other variables. An unsigned integer is allocated 4 bytes of memory for its storage on a typical 32-bit system. Thus, pointers to all types of data occupy the same size of memory because the value of a pointer is the memory address – an unsigned integer. However, the variables to which the pointers point to, of course, occupy different sizes of memory blocks according to their types.
Illustrates that size of pointers to all data types is same
#include<stdio.h> void main() { int x= 38, *ptrx = &x; double y = 5.85, *ptry = &y; char ch = 'H' , *ptrc = &ch; char Name[] = "Dinesh"; char *ptrname = Name; /*& is not used before Name because Name is itself const pointer to the string.*/ clrscr(); printf ("Size of ptrx = %d bytes\n", sizeof(ptrx)); printf ("Size of ptry = %d bytes\n", sizeof(ptry)); printf ("Size of ptrc = %d bytes\n", sizeof(ptrc)); printf ("ptrname = %p \n" , (&ptrname)); printf ("Size of ptrname = %d bytes\n" , sizeof(ptrname)); printf ("Size of x = %d bytes\n" , sizeof(x)); printf ("Size of y = %d bytes\n", sizeof(y)); printf ("Size of ch= %d bytes\n" , sizeof(ch)); printf ("Size of Name= %d bytes\n", sizeof(Name)); printf("Address of x = %p\nAddress of ptrx = %p\n", ptrx, (&ptrx)); printf("Address of c = %p\nAddress of ptrc = %p\n", ptrc, (&ptrc)); printf("Address of y = %p\nAddress of ptry = %p\n", ptry, (&ptry)); }
It is clear from the output that size of memory block allocated for pointers of All types is 2 bytes, because value of a pointer is an unsigned number and on most of 32-bitsystems an unsigned number isallocated4 bytes. However, the variables themselves would occupy different sizes of memory blocks according to the type of a variable. The output also gives the address of x and address of its pointer ptrx in the computer memory (RAM of the computer). The address of x is FFF2 and address of ptrx is FFF0 which is 2 bytes away from FFF0.