Pointer to pointer in C
A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable.
Once a pointer to another pointer is defined, the former pointer contains the address of the latter pointer, which then points to the location that contains the actual value.
Thus, the first pointer is used to store the address of the other variable, while the second pointer is used to store the address of the first variable.
It is due to this very reason they are termed double pointers.
Since a program can have pointers to int, pointers to float, pointers to char, and pointers to any structure in the C standard that are defined, it should not be too much a surprise when it comes to pointers to pointers.
However, when it comes to simple pointers and keeping them as simple as possible, the distinction between the pointer itself and what it points to is essential.

A programmer should be able to think about pointers to other pointers even though we will have the difference between the pointer and what it points to and what that particular pointer points to.
Pointer to pointer can end up to many chain links in a similar way, such as pointers to pointers to pointers or pointers to pointers to pointers, but they do not have any practical use of it.
How is a pointer to pointer declared?
Declaring a pointer to another pointer is similar to declaring a pointer in the C standard. The only other difference is the inclusion of another asterisk before the full syntax.
int **ptr;
Any variable which is a pointer to another pointer must be defined in a similar way.
int **var;
E.g.:
#include <stdio.h>
#include <conio.h>
int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of the variable ‘var’ */
ptr = &var;
/* take the address of pointer ‘ptr’ using the address of operator ‘&’ */
pptr = &ptr;
/* take the value using pptr */
printf("The value of var = %d\n", var );
printf("The value available at *ptr = %d\n", *ptr );
printf("The value available at **pptr = %d\n", **pptr);
return 0;
}
Output:
The value of var = 3000
The value available at *ptr = 3000
The value available at **pptr = 3000
#include<stdio.h>
#include<conio.h>
void main ()
{
int x = 10;
int *p;
int **pp;
p = &x; /* pointer p is now pointing to the address of the variable 'x' */
pp = &p; /*pointer 'pp' is double pointer pointing to the address of the pointer p */
printf("The address of variable 'x' is: %x \n",p); /* Address of 'x' will be printed */
printf("The address of pointer 'p' is: %x \n",pp); /*Address of 'p' will be printed */
printf("The value stored at first pointer 'p' is: %d \n",*p);
/*value stoted at the address contained by p will be printed */
printf("The value stored at the second pointer 'pp': %d \n",**pp);
/*value stored at the address contained by the pointer stored at pp */
}
Output:
The address of variable 'x' is: 6e1cfe34
The address of pointer 'p' is: 6e1cfe38
The value stored at first pointer 'p' is: 10
The value stored at the second pointer 'pp': 10
In the above program, ‘p’ is the pointer variable that can hold the address of the variable ‘x’. In the same way, ‘pp’ is the variable that can hold the address of the variable ‘p’. It can withhold the address of the variable ‘x’.
The pointer ‘*pp’ gives the value of an address stored by the ‘pp’. The pointer ‘pp’ stores the address of the ‘p’ pointer, and the value at the address of ‘p’ is the address of the variable ‘x’.
Hence, ‘*pp’ prints the address of the variable ‘x’. The ‘**pp’, also read as ‘*(*pp),’ gives the stored value at the address ‘*pp’. It can be concluded that ‘*pp’ means the address of the variable ‘x’.
Referencing and dereferencing of a double pointer:
- Referencing: referencing means assigning the address to the pointer declared previously.
- Dereferencing: dereferencing means accessing the value that is stored in the pointer.
E.g.:
#include<stdio.h>
#include<conio.h>
int main()
{
int x = 10;
int *referencing = &x; /* references the variable x */
int **dereferencing = &referencing; /*dereferences the pointer */
printf("The address of x = %p\n",&x);
printf("referencing is pointing to the address = %p\n",referencing);
printf("dereferencing is pointing to the address = %p\n",dereferencing);
printf("The value of x = %d\n",x);
printf("*referencing = %d\n",*referencing);
printf("**dereferencing = %d\n",**dereferencing);
return 0;
}
Output:
The address of x = 0x7fff7c538284
referencing is pointing to the address = 0x7fff7c538284
dereferencing is pointing to the address = 0x7fff7c538288
The value of x = 10
*referencing = 10
**dereferencing = 10