C Double Pointer (Pointer to Pointer)

When working with the C programming language, you may come across the concept of a double pointer, also known as a pointer to a pointer. This concept can be a bit confusing for beginners, but once you understand it, it can be a powerful tool for managing memory and working with complex data structures.

What is a Double Pointer?

A double pointer is a variable that stores the address of another pointer. In other words, it is a pointer to a pointer. Just like a regular pointer, a double pointer also holds the memory address of a variable. However, instead of holding the address of a single variable, it holds the address of another pointer that points to a variable.

Why Use Double Pointers?

Double pointers are commonly used in situations where you need to modify the value of a pointer itself. For example, if you want to pass a pointer to a function and modify the original pointer, you can use a double pointer. By passing the address of the pointer, you can modify its value directly, rather than returning a modified pointer from the function.

Double pointers are also useful when working with dynamic memory allocation. They allow you to allocate memory for a pointer dynamically and then modify the pointer itself.

How to Declare and Use a Double Pointer

Declaring a double pointer is similar to declaring a regular pointer, but with an extra asterisk (*) to indicate that it is a pointer to a pointer. Here is an example:


int** doublePtr;

In this example, we have declared a double pointer named doublePtr that can hold the address of a pointer to an integer.

To assign a value to a double pointer, you need to use the address-of operator (&) to get the address of a pointer. Here is an example:


int* ptr;
int** doublePtr;

int num = 10;
ptr = #
doublePtr = &ptr

In this example, we first declare a regular pointer named ptr and assign it the address of an integer variable num. Then, we declare a double pointer named doublePtr and assign it the address of the pointer ptr.

To access the value stored in a double pointer, you need to use the dereference operator (*) twice. Here is an example:


int* ptr;
int** doublePtr;

int num = 10;
ptr = #
doublePtr = &ptr

printf("Value of num: %dn", **doublePtr);

In this example, we use the doublePtr to access the value stored in num by dereferencing it twice.

A double pointer, or a pointer to a pointer, is a powerful concept in the C programming language. It allows you to work with pointers in a more flexible and dynamic way. By understanding how to declare, assign values to, and access the value stored in a double pointer, you can take advantage of its capabilities and enhance your programming skills.

Scroll to Top