Pointer to a Pointer

From WikiEducator
Jump to: navigation, search

Pointer to a Pointer:

So far we have understood that a pointer is also a special variable, which is having a special role to play. One question that might strike in your head that if a pointer is a variable it must also have some memory allocation with a specific memory address (Hexadecimal address). Yes you are perfectly right. Pointers do have their own addresses just like a simple variable. Now a million dollar question arises that if pointers do have their own memory addresses, then their might be some variable which must be having ability to store memory address of a pointer.
Yes there are special variables which are intended to store the memory addresses of other pointer variables. These special variables are called pointer to a pointer or more precisely Double Pointers. The whole scenario could be explained by following diagram:


Declaring Double Pointer variable:

A double pointer variable can be declared in the same way as a normal pointer variable except that its declaration will involve two * (asterisks). For e.g.

int a = 32 ; //simple variable having value 32
int * aa = &a; // normal pointer variable storing the address of a 
int ** aaa = &aa ; // double pointer storing the address of aa (which is itself  a pointer)

Note : A double pointer is always being assigned the address of another pointer variable , or the value of another double pointer variable. If we try to assign the a value of simple pointer variable into a Double pointer it will cause error. E.g. :

int a = 90 , b = 46 ;
int *p = &a ;
int * q = &b;
int ** r =  p;  // Invalid assignment can’t assign simple pointer to a double pointer
int **s = &p;
int **f = s  // Correct can assign double pointer to another double pointer