Using pointer to pointer in C language

Let’s learn what is pointer to pointer or double pointer in C language. We know that a pointer variable can store the address of other variable. A pointer can also store the address of another pointer. In this guide we will store the address of a pointer variable to another pointer variable. The pointer which can store the address of other pointer is known as double pointer.

Syntax of pointer to pointer (double pointer) is given bellow;

// syntax of double pointer

int **ptr;

// here ptr can store the address of another int type pointer.

Now we will see a C program using double pointer.

Example C program using pointer to pointer

See the program to understand about double pointer in C. Here your concept about pointer to pointer will be cleared.

// pointer to pointer example program in C

#include<stdio.h>
int main(){
  int x = 15;
  int *ptr;
  int **dptr;
  ptr = &x;
  dptr = &ptr;
  printf("Address of x is = %p\n",ptr);
  printf("Address of ptr is = %p\n",dptr);
  printf("Value stored at ptr is = %d\n",*ptr);
  printf("Value stored at dptr is = %d\n",**dptr);
return 0;
}

Output of this double pointer program:

pointer-to-pointer-in-c-program-using-pointer-to-pointer

Another example program of double pointer

See the following program where we have printed the address and value of given variable several times.

// example program using double pointer in C

#include<stdio.h>
int main(){
  int n = 30;
  int *ptr;
  int **ptr2;

  ptr = &n;
  ptr2 = &ptr;

  printf("Address of n is = %p\n", &n);
  printf("Address of ptr is = %p\n", ptr);
  printf("Value of *ptr is = %d\n", *ptr);
  printf("Address of ptr2 is = %p\n", ptr2);
  printf("Value of **p2 is = %d\n", *ptr);
return 0;
}

You will see the output like this

Address of n is = 000000000061FE14
Address of ptr is = 000000000061FE14
Value of *ptr is = 30
Address of ptr2 is = 000000000061FE08
Value of **p2 is = 30

This program may print different address when you try it in your own computer. Don’t worry. It is totally depend on your system.

Previous pagePointer in C

Next pagePointer to function in C