BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C – Pointer to Pointer (Double Pointer) with example

Last Updated: September 21, 2017 by Chaitanya Singh | Filed Under: c-programming

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:
pointer-to-pointer or double pointer

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

Top Related Articles:

  1. Passing pointer to a function in C with example
  2. Two dimensional (2D) arrays in C programming with example
  3. Functions printf() and scanf() in C
  4. Structure in C programming with examples
  5. C – switch case statement in C Programming with example

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Priyaranjan says

    October 24, 2015 at 1:40 PM

    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!

    Reply
  2. Vinayak says

    October 31, 2015 at 9:14 PM

    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.

    Reply
  3. Jason Kinney says

    December 16, 2015 at 9:14 PM

    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?

    Reply
    • srl says

      April 7, 2017 at 2:38 PM

      Wait a minute. The address of pr2 (&pr2) is assigned to the pointer pr1. But pr1’s OWN address is NOT &pr2.

      Reply
  4. vj says

    January 3, 2016 at 2:42 PM

    Yeah jason…me oso thinking the same.
    As per dat logic ..it should be “pr1”.

    Reply
  5. LA Hattersley says

    February 13, 2016 at 7:35 PM

    I think this code is wrong

    /* I’m reading the address of variable num and
    * storing it in pointer pr2*/
    pr2 = #

    I used:
    pr2 = &num;
    To get it working

    Reply
  6. Gajendra Thakre says

    November 17, 2016 at 1:22 PM

    Pointer To Pointer Can Point To A Memory Locations Of A Variable Of Any Data Types.

    Reply
  7. JUH says

    March 19, 2017 at 7:18 AM

    it should be “pr1” instead of “pr1*”

    Reply
  8. Nikhil says

    March 20, 2017 at 4:55 AM

    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.

    Reply

Leave a Reply Cancel reply

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

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap