Published: 26 Nov 2025 | Reading Time: 6 min read
This comprehensive guide covers:
By the end of this guide, you will be able to use double pointers in coding situations with real-life applications and avoid common pointer errors.
"Master the pointer, and C reveals its hidden doors."
Every C programmer hears this advice, yet pointers remain one of the most confusing topics until you understand how memory really works.
If you want to build dynamic data structures, manage memory efficiently, or work with multi-dimensional arrays, learning pointer to pointer in C is a must-have skill. Many real-world systems, from operating systems to embedded devices, depend on these concepts.
In this guide, you will explore double pointers with simple explanations, memory diagrams, and real code examples. You'll learn how they work, where they're used, and how to avoid common pointer errors so you can write safer, more powerful C programs.
A pointer is a uniquely different kind of variable in C that keeps the memory address of another variable. It is like a pointer is the one, which points to the place where the data is stored. In this way, it is possible to have more efficient memory management and hence great new features like dynamic memory allocation, function pointers, and data structure manipulation can be used.
A pointer in C is created by using the asterisk * symbol, which comes next to the pointer name and type. Next, the address-of operator & is used to assign a variable's address.
Use the * operator before the variable name to declare a pointer:
data_type *pointer_name;
int *ptr;
In this case, the pointer ptr is created to hold the address of an integer variable.
#include <stdio.h>
int main() {
int number = 10; // Declare and initialize an integer variable
int *ptr; // Declare a pointer to an integer
ptr = &number; // Assign the address of 'number' to the pointer
// Display the value and address information
printf("Number value: %d\n", number);
printf("Address of number: %p\n", &number);
printf("Pointer 'ptr' stores: %p\n", ptr);
printf("Value at the address stored in ptr: %d\n", *ptr);
return 0;
}
Number value: 10
Address of number: 0x7ffcd3920264
Pointer 'ptr' stores: 0x7ffcd3920264
Value at the address stored in ptr: 10
In Pointer to Pointer in C, a pointer is considered as a variable that stores the memory address of another variable. To declare a pointer, you use the * symbol, which signifies that the variable being declared will store a memory address rather than an actual value.
data_type *pointer_name;
Pointers let you work directly with memory, giving your programs more speed, flexibility, and control. Once you understand how to declare, assign, and dereference them, you unlock the foundation for mastering double pointers, dynamic memory allocation, and advanced data structures in C.
In C, a pointer to an array is essentially a pointer that records the location of the first element of the array. An array name, when used in an expression, is by default considered a pointer to its first element. Hence, you can use the pointer to indirectly access and change the array elements, thus making the process of traversal and element updating more efficient and convenient.
The declaration of a pointer to an array can be done by means of the following syntax:
data_type (*pointer_name)[size_of_array];
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5}; // Array of integers
int (*ptr)[5] = &arr; // Pointer to the array arr (size 5)
// Accessing elements using the pointer
printf("First element: %d\n", (*ptr)[0]);
printf("Second element: %d\n", (*ptr)[1]);
printf("Third element: %d\n", (*ptr)[2]);
printf("Fourth element: %d\n", (*ptr)[3]);
printf("Fifth element: %d\n", (*ptr)[4]);
return 0;
}
arr with 5 numbers: {1, 2, 3, 4, 5}int (*ptr)[5] = &arr; to generate a pointer to the entire array. This means ptr points to the entire array, not just the first element(*ptr)[index], which gets values from the array through the pointerFirst element: 1
Second element: 2
Third element: 3
Fourth element: 4
Fifth element: 5
Time Complexity: O(1) per element access
Space Complexity: O(n) if allocating memory for the array, otherwise O(1) for just storing the pointer
It is not necessary to have a different pointer for each element; one pointer can point to the whole array. By the means of a single pointer int (*ptr)[5], you can reach every element of the array. This technique is handy when you want to transfer big arrays into functions or when you require more explicit control over the multi-dimensional arrays. The accessing of elements is still O(1), and the pointer is not taking any additional spaces other than the reference.
A pointer to a structure in C stores the memory address of a structure variable. Like pointers to basic data types, a pointer to a structure allows indirect access to the structure's members. In C, when you use a pointer to a structure pointer, it allows you to do three things: first, pass the structures to functions in a more efficient way; second, change the structure data inside the functions; and third, get the structure members or change them on the fly.
struct structure_name *pointer_name;
#include <stdio.h>
// Defining a structure called 'Person'
struct Person {
char name[50];
int age;
};
int main() {
// Declaring a structure variable
struct Person person1 = {"John Doe", 30};
// Declaring a pointer to the structure
struct Person *ptr;
ptr = &person1;
// Accessing structure members using the pointer
printf("Name: %s\n", ptr->name); // Using '->' to access members
printf("Age: %d\n", ptr->age);
return 0;
}
Person with two members: name and age. The variable person1 of type Person is then created, and values are assigned to itptr, is created to hold the address of person1. Using the pointer, the code accesses and prints the name and age of person1 using the -> operator, which is used to access structure members through a pointerName: John Doe
Age: 30
Time Complexity: O(1) for access
Space Complexity: O(n), where n is the size of the structure in memory
It is also possible for a pointer to point to an array of elements instead of just a single element. With the help of int (*ptr)[5], one can reach out to the whole array from a single pointer. The usage of this method is handy when you want to pass a big array to a function or when you need better control over the multi-dimensional arrays. The access to elements is still O(1), and the pointer itself is not taking any additional space apart from the reference.
A pointer to pointer (also called a double pointer) is a pointer that stores the memory address of another pointer. It allows indirect access to a variable through multiple levels of indirection.
Understanding how to correctly declare and initialize a double pointer is essential before using it in memory allocation, function arguments, or multi-level indirection.
A double pointer is declared by using two asterisks (**) before the variable name. It informs the compiler that rather than storing a direct value, this variable will hold the address of another pointer.
pointer_data_type **variable_name;
int **dptr;
Here:
int → data type pointed to at the final level* → first level pointer** → second level pointer (double pointer)Initialization requires a pointer whose address can be stored inside the double pointer.
int var = 20;
int *ptr = &var; // single pointer holds address of var
int **dptr = &ptr; // double pointer holds address of ptr
var stores a valueptr stores the address of vardptr stores the address of ptr**dptr → 20To understand how a double pointer works, let's break it down:
A single pointer holds the address of a variable:
int num = 10;
int *ptr = #
Here, ptr holds the address of num.
A double pointer holds the address of a pointer:
int **dptr = &ptr;
Now, dptr holds the address of ptr, which in turn holds the address of num.
To access the value of num through dptr, you need to dereference it twice:
printf("%d", **dptr); // Outputs 10
dptr to get ptrptr to get the value of numnum and assign it a valueptr that can hold the address of an integerdptr that can hold the address of another pointernum in ptrptr in dptrnum using:*ptr (single dereference)**dptr (double dereference)#include <stdio.h>
int main() {
int number = 10; // A normal integer variable
int *ptr; // Pointer to an integer
int **dptr; // Pointer to pointer
ptr = &number; // Storing address of num in ptr
dptr = &ptr; // Storing address of ptr in dptr
printf("Value of number: %d\n", number);
printf("Address of number: %p\n", &number);
printf("Final value accessed through dptr: %d\n", **dptr);
return 0;
}
This C program demonstrates how double pointers work. It starts by declaring an integer variable number with a value of 10. Then, it creates a pointer ptr that stores the address of number. Next, a double pointer dptr is declared, which stores the address of ptr. When **dptr is used, it means accessing the value stored at the address pointed to by ptr, which is in turn pointed to by dptr. So, **dptr ultimately gives the value of number. The program displays the value of num, its memory address, and verifies that **dptr retrieves the same value, confirming correct access through the double pointer.
Value of number: 10
Address of number: 0x7ffeefbff67c
Final value accessed through dptr: 10
Since a double pointer is a concept, not an algorithm, it doesn't have a defined time complexity. However, when used in operations like dynamic memory allocation or 2D arrays, we can discuss complexity based on use case:
Time Complexity:
**ptr): O(1) — Constant time to dereference a double pointer and access the valueSpace Complexity:
+-----+ +-----+ +-----+
| a | ---> | ptr | ---> | dptr|
+-----+ +-----+ +-----+
10 Address of 'a' Address of 'ptr'
a holds the value 10ptr holds the address of adptr holds the address of ptrIn C, a double pointer, or pointer to pointer, has the same size as a single pointer. The system architecture determines this size:
Whatever data type the pointer points to, it always has the same size. For example, on the same system, int **ptr and char **ptr both take up the same amount of memory.
The breadth of the system's memory addresses determines the size, not the data being referenced. As a result, the size of a double pointer is specified by the design of the system and is constant across various data types.
Example on a 64-bit system:
#include <iostream>
int main() {
int num = 10;
int* pointer = # // Pointer to num
int** doublePointer = &pointer; // Double pointer pointing to pointer
// Printing the size of pointer and double pointer
std::cout << "Size of pointer: " << sizeof(pointer) << " bytes" << std::endl;
std::cout << "Size of double pointer: " << sizeof(doublePointer) << " bytes" << std::endl;
return 0;
}
Size of ptr: 8 bytes
Size of dptr: 8 bytes
&). For example, ptr = # assigns the address of num to ptr. Similarly, dptr = &ptr; assigns the address of ptr to dptr*). For a double pointer:*dptr gives the value of ptr, or the address of num**dptr gives the value of numThis implies that you can retrieve the original value that a double pointer ultimately points to by dereferencing it twice.
**dptr&, while dereferencing uses * and ** to reach the original valueDouble pointers in C are one of the great tools which make it possible to implement advanced data structures and manage memory efficiently. Some of their applications are as follows:
Dynamic Memory Allocation for Multi-Dimensional Arrays: Double pointers are crucial for memory allocation in 2D arrays because they enable flexible and dynamic memory management
Passing Pointers to Functions: By using them, functions get the ability to change the original pointer, which is a great feature for dynamic memory allocation or data structures updating
Implementing Linked Data Structures: Double pointers are commonly used to implement linked lists, trees, and other hierarchical data structures, facilitating the manipulation of node pointers
Function Pointers for Callback Mechanisms: Double pointers can be used to manage arrays of function pointers in more complex situations, allowing callback and dynamic function dispatch
A pointer to pointer is particularly useful when dealing with dynamic memory allocation for multi-dimensional arrays or when you need to modify the address of a pointer within a function.
Functions like malloc(), calloc(), and realloc() are used in C to allocate memory dynamically. When using a pointer to pointer, you can allocate memory for a 2D array or manage memory dynamically for complex data structures.
Here's an example that shows how to dynamically allocate memory for a 2D array using a pointer to pointer:
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3;
int cols = 4;
// Declare an array of numbers as the pointer to another pointer.
int **arr;
// Dynamically allocate memory for 'rows' number of pointers
arr = (int **)malloc(rows * sizeof(int *)); // Allocates memory for an array of 'rows' pointers
// Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Dynamically allocate memory for 'cols' number of integers for each row
for (int i = 0; i < rows; i++) {
arr[i] = (int *)malloc(cols * sizeof(int)); // Allocates memory for each row
if (arr[i] == NULL) {
printf("Memory allocation for row %d failed!\n", i);
return 1;
}
}
// Assigning values to the 2D array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = (i + 1) * (j + 1); // Just an example, multiply row and column indices
}
}
// Printing the 2D array
printf("2D Array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
// Free the dynamically allocated memory
for (int i = 0; i < rows; i++) {
free(arr[i]); // Free memory for each row
}
free(arr); // Free memory for the array of pointers
return 0;
}
To create a 2D array, the program begins by declaring a double pointer arr. It dynamically allocates memory for multiple pointers, with each one pointing to a row in the array. Then, it assigns memory for the columns in each row separately. This method enables flexible memory management, allowing each row of the 2D array to be handled independently.
Next, the program assigns values to each element in the 2D array using nested loops. Each element arr[i][j] is assigned the product of (i + 1) and (j + 1), effectively filling the array with a multiplication table.
After populating the array, the program prints its contents in a matrix format. Finally, it frees the allocated memory for each row and then the array of pointers itself to prevent memory leaks.
This approach provides flexibility in managing 2D arrays, especially when the size of the array is determined at runtime.
2D Array:
1 2 3 4
2 4 6 8
3 6 9 12
Time Complexity: O(rows * cols) for allocation, O(rows) for deallocation
Space Complexity: O(rows * cols) for the array
Double pointers make dynamic 2D memory allocation possible by giving full control over row-by-row allocation. This approach provides flexibility, supports runtime-defined sizes, and ensures efficient memory management, as long as you remember to free every block you allocate.
In C, a pointer to pointer is a variable that is used for changing the original pointer inside a function. This is necessary because a single pointer only allows you to modify the value at the address it points to. Still, a pointer to pointer allows you to modify the pointer itself (the memory address stored in the pointer).
return_type function_name(data_type **pointer);
In this example, we will dynamically allocate memory for an integer array inside a function using a pointer to pointer.
#include <stdio.h>
#include <stdlib.h>
// Function to allocate memory for an array and initialize values
void allocateAndInitialize(int **arr, int size) {
*arr = (int *)malloc(size * sizeof(int));
// Check if memory allocation was successful
if (*arr == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
for (int i = 0; i < size; i++) {
(*arr)[i] = i + 1;
}
}
int main() {
int *arr = NULL; // Declare a pointer to int, initially NULL
int size = 5; // Size of the array to be allocated
// Call the function with a pointer to the pointer
allocateAndInitialize(&arr, size);
// Print the values of the dynamically allocated array
printf("Array values: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Free the dynamically allocated memory
free(arr);
return 0;
}
main(), an integer pointer arr is declared and initialized to NULLallocateAndInitialize() is called with the address of arr (i.e., a pointer to a pointer)malloc() to hold 5 integersmain(), the initialized array values are printedfree() so as not to have memory leaksArray values: 1 2 3 4 5
When a pointer to a pointer is used as a function argument, it allows the function to change the original pointer, which is something that a regular pointer cannot do. Basically, it is necessary for operations involving dynamic memory allocation, the efficient passing of arrays, and the retention of changed values outside the function.
In C, multilevel pointers are pointers that point to other pointers, allowing for multiple levels of indirection. These are also referred to as double pointers, triple pointers, and so on, based on the number of indirection levels.
A triple pointer in C is a pointer that holds the address of a double pointer, which in turn holds the address of a single pointer. This creates three levels of indirection, allowing access to a variable through multiple pointers.
data_type ***ptr;
data_type is the type of data the pointer ultimately points toptr is the triple pointer variableBEGIN
DECLARE integer variable num
SET num = 10
DECLARE pointer ptr to integer
SET ptr to address of num
DECLARE pointer dptr to pointer to integer
SET dptr to address of ptr
DECLARE pointer tptr to pointer to pointer to integer
SET tptr to address of dptr
PRINT value at address pointed to by tptr (dereference three times)
END
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
int **dptr = &ptr;
int ***tptr = &dptr;
// Accessing the value of num using the triple pointer
printf("Value of num: %d\n", ***tptr);
return 0;
}
num is a simple integer variablenum is stored in a pointer called ptrptr is stored in a double pointer called dptrtptr is a triple pointer that holds the address of dptrnum through tptr, we dereference it three times: ***tptrValue of num: 10
Multilevel pointers extend indirection by letting one pointer reference another. A triple pointer (***ptr) points to a double pointer, enabling access or modification through three levels. This structure is useful in advanced memory management, nested data structures, and scenarios where deeper pointer manipulation is required.
Using Uninitialized Pointers: If you declare a pointer without initializing it, it may dereference to undefined behaviour. Always initialize pointers before use
Dereferencing NULL Pointers: Accessing memory through a NULL pointer results in a segmentation fault. Ensure pointers are not NULL before dereferencing
Memory Leaks: When dynamically allocated memory is not released, memory leaks occur. When releasing memory, always use malloc or calloc in conjunction with free
Dangling Pointers: If a pointer is freed but still retains the address of that memory, then it is a dangling pointer. To be safe from any unintentional access, set pointers to NULL after freeing
Double Free Errors: The act of freeing the same piece of memory twice on a system may cause the memory management system to become corrupted. It is a good practice to check that a memory block that has been allocated is freed only once
Understanding how pointers to pointers in C work is essential for mastering advanced programming concepts. It provides more precise management of dynamic memory, makes it possible to build intricate data structures, and deepens pointer capabilities. Learning different kinds of pointers, for instance, pointers to objects in C, pointers to arrays, pointer to structures in C, and pointers to functions in C, will have a great impact on your proficiency in writing efficient and memory-optimized C programs.
A double pointer (or pointer to pointer) is a variable that keeps the address of another pointer. This allows access to a value through multiple levels of indirection.
A double pointer is declared using two asterisks.
For example:
int **dptr;
Double pointers are used when you need to modify the address stored in a pointer (such as allocating memory inside a function), for dynamic multi-dimensional arrays, or for advanced data structures like linked lists.
Dereference twice:
*dptr gives the address stored in the first pointer**dptr gives the value stored at that addressDouble pointers allow functions to change the value of a pointer passed to them, not just the data it points to. This is essential for dynamic memory allocation and for building complex data structures.
A double pointer (**ptr) stores the address of a pointer, while an array of pointers (e.g., int *arr[10]) is a collection of pointers stored in contiguous memory. The usage and memory layout are different.
No. Static 2D arrays (e.g., int arr[3][4]) do not require double pointers. Double pointers are needed for dynamic 2D arrays where the size is determined at runtime.
Yes. You can return a double pointer, often after dynamically allocating memory for a 2D array or other data structure.
About NxtWave
NxtWave is a learning platform offering industry-recognized certifications and comprehensive programming courses. Contact: [email protected] | WhatsApp: +919390111761