C

C – Example Of Pointer To Pointer (Double Pointer)

We already know that a pointer is a variable that holds the address of another variable of the same type. When a pointer holds the address of another pointer, it is referred to as a pointer-to-pointer or double pointer. This guide will teach you what a double pointer is, how to declare one, and how to use one in C programming. To grasp this concept, you should be familiar with the fundamentals of pointers.

In C, how do you declare a Pointer to Pointer (Double Pointer)?

int **pr;

Pr is a double pointer in this case. The declaration of a double pointer must contain two *’s.
Let’s look at a diagram to better understand the concept of double pointers:
According to the diagram, pr2 is a regular pointer that stores the address of an integer variable num. In the diagram, there is another pointer pr1 that holds the address of another pointer pr2, and the pointer pr1 here is a pointer-to-pointer (or double pointer).
Values from the preceding diagram:

Variable num has address: XX771230
Address of Pointer pr1 is: XX661111
Address of Pointer pr2 is: 66X123X1

Double Pointer Example

Let’s write a C program based on the diagram we saw earlier.

#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 misunderstandings about the output of this program; when you run it, you will see an address similar to this: 0x7fff54da7c58. I’ve given the address in different formats because I want you to connect this program to the diagram above. I used exact address values in the above diagram so that you can easily relate the output of this program to the above diagram.
You can also understand the program logic by using the following simple equations:

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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button