Pointer to Pointer (Double Pointer) in C

In previous two posts, we learned basics of pointers. We learned to create pointers and how to perform arithmetic operations on them.

We learned to create pointers to int and char. In real, you can have pointer to any type in C. You can have a pointer to int, char, float, double, structure, array or even pointer. In fact, you can declare pointer to pointer to pointer to pointer. That looks complex. For now, let us focus on pointer to pointer.

If a pointer points to a pointer of same type, we call it as pointer to a pointer. Read the statement slowly again, if you did not catch my words. Many texts and authors also refer pointer to pointer as double pointer, since two pointers are involved.

Pointer to Pointer (Double pointer) memory representation
Pointer to Pointer (Double pointer) memory representation

In the above image pointer *ptr points at memory location 0x1230 of integer type. Pointer **dPtr points at memory location 0x1220 of integer pointer type.

Note: Pointer to pointer will always store address of a pointer of same type.

How to declare pointer to pointer (double pointer)

Pointer to a pointer declaration follows same declaration syntax as normal pointer declaration. The only thing you must care is it contains two * asterisks.

In general increase number of * asterisks during pointer declaration to increase pointer level.

Syntax to declare pointer to pointer

data-type ** pointer-variable-name;

Here double ** specifies it as pointer to pointer (double pointer). Increase number of * asterisk with increase in pointing levels. Means for pointer to pointer to pointer use ***.

Example to declare pointer to pointer

int ** ptr;

The above statement declares a pointer to integer pointer type.

How to initialize pointer to a pointer (double pointer)

Like declaration, pointer to pointer initialization is also similar to simple pointer initialization. However, you must be cautious while initializing pointer to pointer. As it, accept address of a pointer variable of same type.

Example to initialize pointer to a pointer

int num    = 10;    // Integer variable
int *ptr   = #  // Pointer to integer

int **dPtr = &ptr;  // Pointer to integer pointer

How to access pointer to a pointer

We use dereference * operator, to access value stored at memory location pointed by a pointer. However, single dereference will not work when dealing with pointer to a pointer. In order to access value pointed by pointer to a pointer we use double dereference ** (indirection) operator.

Example to access pointer to a pointer

int num = 10;        // Declare an integer variable
int *ptr   = #   // Pointer to integer
int **dPtr = &ptr;   // Pointer to integer pointer

printf("Value of dPtr = %d \n", dPtr);   // Print value of dPtr, i.e address of ptr
printf("Value of ptr = %d \n", *dPtr);   // Print value of ptr, i.e. address of num
printf("Value of num = %d \n", **dPtr);  // Print value of num

Example program to use pointer to pointer

Write a C program to demonstrate the use of pointer to a pointer.

/**
 * C program to demonstrate use of pointer to a pointer
 */

#include <stdio.h>

int main()
{
    int num; // Integer variable

    int *ptr   = &num;  // Pointer to integer
    int **dPtr = &ptr;  // Pointer to integer pointer

    /* Change the value of num directly */
    num = 10;
    printf("Value of num   = %d \n", num);
    printf("Value pointed by ptr  = %d \n", *ptr);
    printf("Value pointed by dPtr = %d \n\n", **dPtr);


    /* Assigns 100 using pointer to integer */
    *ptr = 100;
    printf("Value of num   = %d \n", num);
    printf("Value pointed by ptr  = %d \n", *ptr);
    printf("Value pointed by dPtr = %d \n\n", **dPtr);


    /* Assigns 1000 using pointer to integer pointer */
    **dPtr = 1000;
    printf("Value of num   = %d \n", num);
    printf("Value pointed by ptr  = %d \n", *ptr);
    printf("Value pointed by dPtr = %d \n\n", **dPtr);

    return 0;
}

Output –

Value of num   = 10
Value pointed by ptr  = 10
Value pointed by dPtr = 10

Value of num   = 100
Value pointed by ptr  = 100
Value pointed by dPtr = 100

Value of num   = 1000
Value pointed by ptr  = 1000
Value pointed by dPtr = 1000