Home >>C Tutorial >Double Pointers in C
It is a known fact from the previous tutorial that the address of a variable is stored in C using the pointers as they point to a location in the memory. The main advantage of the pointers is that they reduce the access time of the variables. Whenever the pointer to pointer is defined, the address of the variable is stored by the first pointer and the address of the first pointer is stored by the second pointer hence, they are called as the double pointers in C language.
The declaration of the pointer to pointers in the C language is very similar to declaring a pointer in the C language. The only difference in the two is that in double pointers there is a need of extra '*' just before the name of the pointer.
Here is the syntax of the pointer to pointer in the C language:
int **p; // pointer to a pointer in C language that is pointing towards an integer.
Here is a diagram depicted below to make it easy for you to understand the process:
Let's take an example of pointer to pointer:
#include<stdio.h> void main () { int x = 12; int *a; int **aa; a = &x; // pointer a is pointing to the address of x aa = &a; // pointer aa is x double pointer pointing to the address of pointer a printf("address of x: %x\n",a); // Address of x will be printed printf("address of a: %x\n",aa); // Address of a will be printed printf("value stored at a: %d\n",*a); // value stoted at the address contained by a i.e. 10 will be printed printf("value stored at aa: %d\n",**aa); // value stored at the address contained by the pointer stoyred at aa }
This example indicates one pointer points to another pointer address
#include<stdio.h> int main() { int num=10; int *p; int **p2; p=&num ; p2=&p ; printf("Num variable address is = %x \n",&num); printf("p variable address is =%x \n",p); printf("*p variable value is = %d \n",*p); printf("p2 variable address is = %x \n",p2); printf("**p2 variable value is =%d \n",*p); return 0; }