Pointer To Pointer
- Siddharth Sharma
- Nov 7, 2024
- 2 min read
A pointer to pointer (also known as a double pointer) is a pointer that holds the address of another pointer, rather than the address of a data variable. This can extend to multiple levels, like a triple pointer (int*** ptr) and so on. In C and C++, double pointers are useful for handling dynamic memory allocation for multidimensional arrays, managing pointers passed to functions, and building complex data structures.
Declaring a Pointer to Pointer
To declare a pointer to a pointer, use two asterisks (**):
int num = 5;
int* ptr = # // ptr is a pointer to an integer (holds the address of num)
int** ptr2 = &ptr; // ptr2 is a pointer to a pointer (holds the address of ptr)Example: Pointer to Pointer
Let’s go through a basic example demonstrating how a pointer to a pointer works in C:
#include <stdio.h>
int main() {
int num = 10;
int* ptr = # // ptr is a pointer to int, holds the address of num
int** ptr2 = &ptr; // ptr2 is a pointer to a pointer to int, holds the address of ptr
// Display values and addresses at each level
printf("Value of num: %d\n", num); // Outputs 10
printf("Value at ptr (num): %d\n", *ptr); // Dereference ptr to get num, Outputs 10
printf("Value at ptr2 (ptr): %d\n", **ptr2); // Dereference ptr2 twice to get num, Outputs 10
// Display addresses at each level
printf("Address of num: %p\n", (void*)&num);
printf("Address held by ptr (address of num): %p\n", (void*)ptr);
printf("Address held by ptr2 (address of ptr): %p\n", (void*)ptr2);
return 0;
}Explanation:
int num = 10; creates an integer variable num.
int* ptr = # declares a pointer ptr that stores the address of num.
int** ptr2 = &ptr; declares a pointer-to-pointer ptr2 that stores the address of ptr.
**ptr2 dereferences ptr2 twice to reach num.
Output:
Value of num: 10
Value at ptr (num): 10
Value at ptr2 (ptr): 10
Address of num: [address of num]
Address held by ptr (address of num): [same address as above]
Address held by ptr2 (address of ptr): [address of ptr]Use Cases for Pointer to Pointer
Dynamic Memory Allocation: Used to allocate memory for arrays of pointers (e.g., dynamic 2D arrays).
int rows = 3, cols = 4;
int** array = (int**)malloc(rows * sizeof(int*));
for(int i = 0; i < rows; i++) {
array[i] = (int*)malloc(cols * sizeof(int));
}Passing Pointers to Functions: Double pointers allow functions to modify the value of a pointer passed to it. This is commonly used for functions that return dynamic memory or change a pointer.
void allocateMemory(int** ptr) {
*ptr = (int*)malloc(sizeof(int)); // Allocates memory that ptr will point to
**ptr = 42; // Set value at allocated memory
}
int main() {
int* p = NULL;
allocateMemory(&p);
printf("Value after allocation: %d\n", *p); // Outputs 42
free(p); // Free allocated memory
return 0;
}Multidimensional Arrays: Double pointers can be used to manage arrays of pointers (like rows in a 2D array).
Summary
Pointer to Pointer (int**) stores the address of a pointer.
Useful for dynamic memory allocation for multidimensional arrays, passing pointers to functions, and complex data structures.
Double dereferencing (**) is needed to access the original data stored at the location pointed to by a pointer-to-pointer.




Comments