We already know that a pointer holds the address of another variable of same type. When a pointer holds the address of another pointer then such type of pointer is known as pointer-to-pointer or double pointer. In this guide, we will learn what is a double pointer, how to declare them and how to use them in C programming. To understand this concept, you should know the basics of pointers.
How to declare a Pointer to Pointer (Double Pointer) in C?
int **pr;
Here pr is a double pointer. There must be two *’s in the declaration of double pointer.
Let’s understand the concept of double pointers with the help of a diagram:
As per the diagram, pr2 is a normal pointer that holds the address of an integer variable num. There is another pointer pr1 in the diagram that holds the address of another pointer pr2, the pointer pr1 here is a pointer-to-pointer (or double pointer).
Values from above diagram:
Variable num has address: XX771230 Address of Pointer pr1 is: XX661111 Address of Pointer pr2 is: 66X123X1
Example of double Pointer
Lets write a C program based on the diagram that we have seen above.
#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
Priyaranjan says
Awesome explanation with figures!! My concept of pointers has never been so clear. I hope I won’t be confused any more in this field.
Thanks!
Vinayak says
It’s a great explanation. Thanks!!
I just have one query.
Should it not be :
printf(“\n Address of Pointer pr2 using pr1 is:%u”,pr1);
instead of :
printf(“\n Address of Pointer pr2 using pr1 is:%u”,*pr1);
Since *pr1 is the value of pr2 and not its address.
Jason Kinney says
Is there a typo in the code for the section “Ways to find address of pointer”? The “*pr1” should be “pr1”, if &pr2 == pr1, no?
srl says
Wait a minute. The address of pr2 (&pr2) is assigned to the pointer pr1. But pr1’s OWN address is NOT &pr2.
vj says
Yeah jason…me oso thinking the same.
As per dat logic ..it should be “pr1”.
LA Hattersley says
I think this code is wrong
/* I’m reading the address of variable num and
* storing it in pointer pr2*/
pr2 = #
I used:
pr2 = #
To get it working
Gajendra Thakre says
Pointer To Pointer Can Point To A Memory Locations Of A Variable Of Any Data Types.
JUH says
it should be “pr1” instead of “pr1*”
Nikhil says
I think there is some typo in 10th print line:
Address of Pointer pr2 using pr1 is: XX771230 (correct) instead of
Address of Pointer pr2 using pr1 is: 66X123X1 (Wrong) according to the register you take.