Double Pointer (Pointer to Pointer) in C with Example

In this tutorial, you will learn about double pointers, also known as pointer to pointer, in C programming. We will explore what double pointers are, why they are used, how they work in memory, and walk through several examples and use cases to solidify your understanding of this powerful concept in C.

What is a Double Pointer?

In C, a pointer is a variable that stores the address of another variable. A double pointer, as the name suggests, is a pointer that stores the address of another pointer. This means it indirectly points to a value.

Syntax:

int **ptr;

Here, ptr is a double pointer to an integer.​

Double Pointer Example in C

#include <stdio.h>
 
int main() {
    int score = 50;          // score holds the value
    int *mcq = &score;      // mcq points to score
    int **quiz = &mcq;      // quiz points to mcq
 
    // Accessing value directly
    printf("Score: %d\n", score);
 
    // Accessing value using single pointer
    printf("Score via *mcq: %d\n", *mcq);
 
    // Accessing value using double pointer
    printf("Score via **quiz: %d\n", **quiz);
 
    return 0;
}

Output:

advertisement
Score: 50  
Score via *mcq: 50  
Score via **quiz: 50

This program shows how single and double pointers work. It sets score to 50, then points mcq to score and quiz to mcq. It prints the value of score directly, through *mcq, and through **quiz. All give the same result. This shows how you can access data in memory in different ways using pointers.

Why Use Double Pointers?

Double pointers are useful in many situations:

  • Dynamic Memory Allocation: They help when you need to allocate memory for arrays of pointers, like a 2D array.
  • Function Arguments: You can pass a double pointer to a function if you want that function to change the original pointer.
  • Multidimensional Arrays: Double pointers make it easier to handle 2D arrays or arrays of pointers.

Double Pointers in Functions

Passing a pointer to a function allows the function to modify the value at that address. However, if you want the function to modify the pointer itself (e.g., allocate memory), you need to pass a double pointer.​

Free 30-Day Python Certification Bootcamp is Live. Join Now!

Example: Allocating Memory in a Function

#include <stdio.h>
#include <stdlib.h>
 
void allocateMemory(int **ptr) {
    *ptr = (int *)malloc(sizeof(int));
    if (*ptr != NULL) {
        **ptr = 70;
    }
}
 
int main() {
    int *p = NULL;
    allocateMemory(&p);
    if (p != NULL) {
        printf("Allocated value: %d\n", *p);
        free(p);
    }
    return 0;
}

Output:

Allocated value: 70

This program uses a function to allocate memory using a double pointer. The allocateMemory function assigns memory and sets the value to 70. In main(), the pointer p gets memory, prints the value, and then frees it. This shows how to safely manage dynamic memory in C.

Double Pointers and Multidimensional Arrays

Double pointers are commonly used to handle dynamic 2D arrays.​

Example: Dynamic 2D Array

#include <stdio.h>
#include <stdlib.h>
 
int main() {
    int rows = 3, cols = 4;
    int **matrix;
 
    // Allocate memory
    matrix = (int **)malloc(rows * sizeof(int *));
    for (int i = 0; i < rows; i++) {
        matrix[i] = (int *)malloc(cols * sizeof(int));
    }
 
    // Initialize and print
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            matrix[i][j] = i * cols + j;
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
 
    // Free memory
    for (int i = 0; i < rows; i++) {
        free(matrix[i]);
    }
    free(matrix);
 
    return 0;
}

Output:

0 1 2 3 
4 5 6 7 
8 9 10 11

This program creates a 2D dynamic array using pointers in C. It first allocates memory for 3 rows, and for each row, it allocates space for 4 columns. Then, it fills the matrix with values using a simple formula and prints them row by row. Finally, it frees the memory to avoid leaks. This shows how to manage dynamic 2D arrays with malloc() and free().

advertisement

Common Mistakes with Double Pointers

  • Uninitialized Pointers: Always initialize pointers before use to avoid undefined behavior.​
  • Memory Leaks: Ensure that every malloc has a corresponding free to prevent memory leaks.​
  • Incorrect Dereferencing: Be cautious with the number of dereferences; *ptr vs. **ptr can lead to bugs if misused.​

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

advertisement
advertisement
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.