What Is Double-Pointer In C? How Is It Work?

What Is Double-Pointer In C? How Is It Work?
Techiio-author
Written by Shuvhojit DebFebruary 6, 2022
10 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this tutorial, we'll know what a double-pointer is and how it works.

Double Pointer in C

A pointer is used to store the address of a variable in C. Pointer reduces the access time of a variable. However, In C, we can also define a pointer to store the address of another pointer. Such a pointer is known as a double pointer (pointer to pointer). The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer.

Algorithm

Begin
   Declare v of the integer datatype.
      Initialize v = 76.
   Declare a pointer p1 of the integer datatype.
   Declare another double-pointer p2 of the integer datatype.
   Initialize p1 as the pointer to variable v.
   Initialize p2 as the pointer to variable p1.
   Print “Value of v”.
      Print the value of variable v.
   Print “Value of v using single pointer”.
      Print the value of pointer p1.
   Print “Value of v using double pointer”.
      Print the value of double pointer p2.
End.

How does Double Pointer work in C?

In this article, we will see how to declare double-pointer with syntax and example, and also we will see how to use them in the C programming language. So let us start from the syntax.

Syntax:

int **pointer_var;

In the above syntax, we can see the variable pointer_var is prefixed with two stars (**), also known as the indirection operator (*) for declaring the double-pointer.

So in general if the pointer is pointing to or referring to an object in memory then a double-pointer is a pointer that would be pointing to or referring to another point where it is pointing to an object in memory. Let us see how this exactly works by below example and pictorial form:

blogpost

Example:

Let's see an example of a double-pointer.

#include <stdio.h>
int main()
{
     int num=123;
     //A normal pointer pr2
     int *pr2;
     //This pointer pr2 is a double pointer
     int **pr1;
     /* Assigning the address of variable num to the
      * pointer pr2
      */
     pr2 = #
     /* Assigning the address of pointer pr2 to the
      * pointer-to-pointer pr1
      */
     pr1 = &pr2;
     /* Possible ways to find value of variable num*/
     printf("\n Value of num is: %d", num);
     printf("\n Value of num using pr2 is: %d", *pr2);
     printf("\n Value of num using pr1 is: %d", **pr1);
     /*Possible ways to find address of num*/
     printf("\n Address of num is: %p", &num);
     printf("\n Address of num using pr2 is: %p", pr2);
     printf("\n Address of num using pr1 is: %p", *pr1);
     /*Find value of pointer*/
     printf("\n Value of Pointer pr2 is: %p", pr2);
     printf("\n Value of Pointer pr2 using pr1 is: %p", *pr1);
     /*Ways to find address of pointer*/
     printf("\n Address of Pointer pr2 is:%p",&pr2);
     printf("\n Address of Pointer pr2 using pr1 is:%p",pr1);
     /*Double pointer value and address*/
     printf("\n Value of Pointer pr1 is:%p",pr1);
     printf("\n Address of Pointer pr1 is:%p",&pr1);
     return 0;
}

Output:

Value of num is: 123
Value of num using pr2 is: 123
Value of num using pr1 is: 123
Address of num is: XX771230
Address of num using pr2 is: XX771230
Address of num using pr1 is: XX771230
Value of Pointer pr2 is: XX771230
Value of Pointer pr2 using pr1 is: XX771230
Address of Pointer pr2 is: 66X123X1
Address of Pointer pr2 using pr1 is: 66X123X1
Value of Pointer pr1 is:  66X123X1
Address of Pointer pr1 is: XX661111
There are some confusions regarding the output of this program, when you run this program you would see the address similar to this: 0x7fff54da7c58. The reason I have given the address in different format is because I want you to relate this program with the diagram above. I have used the exact address values in the above diagram so that it would be easy for you to relate the output of this program with the above diagram.

You can also understand the program logic with these simple equations:

num == *pr2 == **pr1
&num == pr2 == *pr1
&pr2 == pr1

Conclusion

In this article, we can conclude that pointers are also variables that can store values. But a pointer usually stores the value as the address of another variable. So commonly we can define double-pointer as pointer to pointer, which means a pointer stores the address of another pointer. In this way, double pointers are used in allocating the memory or assigning the value as the address of another pointer to access the value even outside the function call bypassing the reference to the argument using ** arg.

Double Pointer
C Language
Algorithm
Programming
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance