C - Pointer to Pointer (Double Pointer)

Last Updated : 18 Jul, 2026

A double pointer in C is a pointer that stores the address of another pointer. It is also known as a pointer to a pointer because it points to another pointer instead of directly pointing to a variable.

  • The first pointer stores the address of a variable, while the double pointer stores the address of that first pointer.
  • Double pointers are commonly used for dynamic memory allocation, passing pointers to functions, and working with multidimensional data structures.
C
#include <stdio.h>

int main()
{

    // A variable
    int var = 10;

    // Pointer to int
    int *ptr1 = &var;

    // Pointer to pointer (double pointer)
    int **ptr2 = &ptr1;

    printf("var: %d\n", var);
    printf("*ptr1: %d\n", *ptr1);
    printf("**ptr2: %d", **ptr2);

    return 0;
}

Output
var: 10
*ptr1: 10
**ptr2: 10

Explanation

  • ptr1 stores the address of the integer variable var, while ptr2 stores the address of the pointer ptr1.
  • **ptr2 first accesses ptr1 and then dereferences it to retrieve the value stored in var.
double-pointers-in-c

Size of Double Pointer

Size of a double pointer is same as size of any other pointer as it also stored address.

C
#include <stdio.h>

int main()
{

    // Defining single and double pointers
    int a = 5;
    int *ptr = &a;
    int **d_ptr = &ptr;

    // Size of double pointer
    printf("%d bytes\n", sizeof(d_ptr));
    
    
    // Size of a single pointer
    printf("%d bytes", sizeof(ptr));

    return 0;
}

Output
8 bytes
8 bytes

Note: The output of the above code also depends on the type of machine which is being used.

Create a Dynamic 2D Array

Double pointers are useful in creating a dynamically sized 2D array where every row can have different number of elements.

C
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int m = 2, n = 3;

    // Create a double pointer
    int **arr;

    // Allocate memory for rows
    arr = (int **)malloc(m * sizeof(int *));

    // Allocate memory for each row
    for (int i = 0; i < m; i++)
        arr[i] = (int *)malloc(n * sizeof(int));

    // Initialize with some values
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            arr[i][j] = i * n + j + 1;
        }
    }

    // Print the array
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }

    // Free allocated memory
    for (int i = 0; i < m; i++)
    {
        free(arr[i]);
    }
    free(arr);

    return 0;
}

Output
1 2 3 
4 5 6 

Explanation

  • A 2D array consists of multiple 1D arrays arranged in rows and columns.
  • A double pointer can be used to create a dynamic 2D array, where it points to an array of row pointers, and each row is allocated separately using malloc().

Pass Array of Strings to Function

C
#include <stdio.h>

// Function that takes array of strings as argument
void print(char **arr, int n)
{
    for (int i = 0; i < n; i++)
        printf("%s\n", *(arr + i));
}

int main()
{
    char *arr[10] = {"Geek", "Geeks", "Geekfor"};
    print(arr, 3);
    return 0;
}

Output
Geek
Geeks
Geekfor

Explanation

  • An array of strings is typically represented as an array of pointers, where each pointer stores the address of a string.
  • Such arrays can be passed to functions using a double pointer (char **), allowing the function to access and manipulate multiple strings efficiently.

Application of Double Pointers in C

Following are the main uses of pointer to pointers in C:

  • They are used in the dynamic memory allocation of multidimensional arrays.
  • They can be used to store multilevel data such as the text document paragraph, sentences, and word semantics.
  • They are used in data structures to directly manipulate the address of the nodes without copying.
  • They can be used as function arguments to manipulate the address stored in the local pointer.

Multilevel Pointers in C

A multilevel pointer is a pointer that points to another pointer. C supports multiple levels of pointers, such as double pointers, triple pointers, and beyond, depending on the program's requirements.

  • A triple pointer (int ***ptr) stores the address of a double pointer, which in turn points to a single pointer.
  • Multilevel pointers are commonly used in complex data structures, dynamic memory allocation, and functions that need to modify multiple levels of pointers.
C++
#include <stdio.h>

int main()
{
    int num = 10;
    int *ptr = &num;
    int **ptr2 = &ptr;

    // accessing values
    printf("Value of num: %d\n", num);
    printf("Value using single pointer: %d\n", *ptr);
    printf("Value using double pointer: %d\n", **ptr2);

    return 0;
}

Output
Value of num: 10
Value using single pointer: 10
Value using double pointer: 10

Note: We can use any level pointer in C. There is no restriction about it but it makes the program very complex and vulnerable to errors.

Comment