Double Pointer (Pointer to Pointer) in C with Example

This C Tutorial Explains Pointer to Pointer or Double Pointer in C Programming with Examples.

What is a Double Pointer in C?

Basically, a pointer holds address; address of some variable, pointer variable, function, structure, array etc. When pointer holds address of some other pointer variable it’s called pointer-to-pointer or double pointer.

Syntax for a Double Pointer

int **ptr; //This declares a double pointer to an integer

Example 1: How does Double Pointer work in C?

/* ptr2ptr1.c -- Program shows double pointer, it's declaration and uses */
#include <stdio.h>
int main(void)
    int a = 50;   /* 'a' an integer initialized value 50 */
    int *b = &a;  /* b is pointer-to-integer */
    int **c = &b; /* c is pointer-to-pointer-to-int or double pointer */
    printf("\nValue of integer a in exp. \"a = 50\" is %d\n", a);
    printf("O key! After exp. \"int *b = &a\", value of a using pointer"
           " *b is %d\n", *b);
    printf("\nNow, we use double pointer **c in exp. \"int **c =  &b\" "
           "to find\nvalue of a as %d\n", **c);
    printf("\nO key! We now try to modify integer a using double pointer"
           " **c.\n");
    **c = 2 * a;
    printf("After \"**c = 2 * a\", value of a %d\n", **c);
    return 0;

Notice the output below:

Value of integer a in exp. "a = 50" is 50
O key! After exp. "int *b = &a", value of a using pointer *b is 50
Now, we use double pointer **c in exp. "int **c = &b" to find
value of a as 50
O key! We now try to modify integer a using double pointer **c.
After "**c = 2 * a", value of a 100

So, what you observed while accessing integer variable ‘a’ using double pointer ‘**c’ and further modified ‘a’ using **c? Let’s understand this. ‘*b’ is a pointer to an integer, here to integer ‘a’.

    int *b = &a;    /* *b points to int a */

And, ‘**c’ is a pointer-to-pointer-to-integer i.e.

Note: Join free Sanfoundry classes at Telegram or Youtube
    int **c = &b;    /* **c is assigned address of pointer *b */

O key! We further unravel ‘**c’. What is given below segment of code, for example,

    c;               /* ? */
    *c;              /* ? */
    **c;             /* this is value of integer a */
    printf("c is a double pointer and have address as %p.\n", c);
    printf("*c is value at address (%p) is %p\n", c, *c);
    printf("**c is a value of type int, which is %d\n", **c);

Let’s understand the output,

    c is a double pointer and have address as 0x7fffbc1f73a8.
    *c is value at the address (0x7fffbc1f73a8) is 0x7fffbc1f73b4
    **c is a value of type int, which is 100

Notice that c is a pointer-to-pointer-to-integer, so c holds address of another pointer, here we say ‘b’. What happens when we perform indirection ‘*’ on c. This causes go to the location of pointer ‘*b’ and access the value in that location i.e. address of integer ‘a’ which was assigned to pointer ‘*b’. And when we further perform indirection on ‘*c’ i.e. ‘*(*c)’, we accessed integer ‘a’.

We make an inference from above that as many levels of pointer to pointer we add, an equal no. of ‘*’ operators precede the pointer.

Example 2:

#include <stdio.h>
void main()
    int k = 5;
    int *p = &k;
    int **m  = &p;
    printf("%d%d%d\n", k, *p, **m);

Program Output:

5 5 5

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.