What Is Pointer to Pointer in Data Structure?


Scott Campbell

A pointer is a variable that holds the memory address of another variable. It allows us to indirectly access and manipulate the value stored in the memory location pointed to by the pointer.

But what if we need to store the memory address of a pointer itself? This is where the concept of a pointer to a pointer, also known as a double pointer, comes into play.

What is a Pointer to Pointer?

A pointer to a pointer is simply a variable that stores the memory address of another pointer. In other words, it points to the memory location where another pointer is stored.

Declaring a double pointer is similar to declaring regular pointers. We use two asterisks (**) before the variable name to indicate that it will be a double pointer.

int** ptrToPtr;

Why Use Pointer to Pointer?

The use of double pointers becomes particularly useful in scenarios where we need to modify or update a pointer from within a function. When we pass an ordinary single-level pointer as an argument, any changes made inside that function only affect the copy of the original pointer rather than modifying its actual value.

However, by passing a double pointer as an argument, we can modify both the original and intermediate pointers. This allows us to update the original pointer’s value and effectively pass it back out from the function.

Example Usage

To better understand how double pointers work, let’s consider an example:

#include <stdio.h>
void updatePointer(int** ptr)
    *ptr = NULL;

int main()
    int num = 5;
    int* ptr = #

    printf("Before: %p\n", ptr);
    printf("After: %p\n", ptr);

    return 0;

In this example, we have a function called updatePointer(), which takes a double pointer as an argument. Inside the function, we set the pointed value to NULL.

In the main function, we declare an integer variable num, and then create a single-level pointer ptr to point to that variable. We pass the address of the pointer variable using the ampersand operator (&) to the updatePointer() function.

The output of this program would be:

Before: 0x7ffeeef0a9a8
After: (nil)

We can see that after calling the function and passing the double pointer, it modifies the original pointer’s value to NULL.


A pointer to a pointer provides us with additional levels of indirection and allows us to modify pointers from within functions. This can be particularly useful in situations where we need to update a pointer’s value and pass it back out from a function.

The proper use of pointers and double pointers is crucial for efficient memory management and manipulation in data structures and algorithms.

Suggested Reading:

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy