In C programming language, pointer to pointer or double pointer is a variable that holds the address of another pointer.
Declaration
Given below is the declaration for pointer to pointer −
datatype ** pointer_name;
For example, int **p;
Here, p is a pointer to pointer.
Initialization
‘&’ is used for initialization.
For example,
int a = 10; int *p; int **q; p = &a;
Accessing
Indirection operator (*) is used for accessing
Sample program
Following is the C program for double pointer −
#include<stdio.h> main ( ){ int a = 10; int *p; int **q; p = &a; q = &p; printf("a =%d ",a); printf(" a value through pointer = %d", *p); printf(" a value through pointer to pointer = %d", **q); }
Output
When the above program is executed, it produces the following result −
a=10 a value through pointer = 10 a value through pointer to pointer = 10
Example
Now, consider another C program which shows the relationship between pointer to pointer.
#include<stdio.h> void main(){ //Declaring variables and pointers// int a=10; int *p; p=&a; int **q; q=&p; //Printing required O/p// printf("Value of a is %d\n",a);//10// printf("Address location of a is %d\n",p);//address of a// printf("Value of p which is address location of a is %d\n",*p);//10// printf("Address location of p is %d\n",q);//address of p// printf("Value at address location q(which is address location of p) is %d\n",*q);//address of a// printf("Value at address location p(which is address location of a) is %d\n",**q);//10// }
Output
When the above program is executed, it produces the following result −
Value of a is 10 Address location of a is 6422036 Value of p which is address location of a is 10 Address location of p is 6422024 Value at address location q(which is address location of p) is 6422036 Value at address location p(which is address location of a) is 10