Pointer to Pointer in C (Double Pointer)
As we know that, a pointer is used to store the address of a variable in C. Pointer reduces the access time of a variable. However, In C, we can also define a pointer to store the address of another pointer. Such pointer is known as a double pointer (pointer to pointer). The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer.
A pointer to a pointer, often referred to as a double pointer, is a fundamental concept in C that allows you to have a pointer pointing to another pointer. This concept is commonly used when working with dynamic memory allocation, multi-dimensional arrays, or when you need to modify a pointer from a function and have those changes reflected in the calling function
.Let's understand it by the diagram given below.
syntax of declaring a double pointer in C:
cdata_type **double_pointer_variable;
data_type: The data type to which the double pointer points. It could be any valid C data type, including another pointer type.
double_pointer_variable: The name of the double pointer variable.
Double pointer in C example:
c#include <stdio.h>
int main() { int num1 = 42; int num2 = 99;
int *ptr1 = &num1; int *ptr2 = &num2;
int **doublePtr1 = &ptr1; // Double pointer pointing to ptr1 int **doublePtr2 = &ptr2; // Double pointer pointing to ptr2
printf("Value of num1: %d\n", num1); printf("Value of num2: %d\n", num2);
// Modify values using double pointers **doublePtr1 = 50; **doublePtr2 = 77;
printf("Modified value of num1 using double pointer: %d\n", num1); printf("Modified value of num2 using double pointer: %d\n", num2);
return 0;}
Output:
arduinoValue of num1: 42Value of num2: 99Modified value of num1 using double pointer: 50Modified value of num2 using double pointer: 77
Declaration of Pointer to a Pointer in C
1. Declaring a pointer to a pointer to an integer:
cint **intDoublePtr;
In this case, intDoublePtr is a double pointer that can point to an int or to a pointer to an int.
2. Declaring a pointer to a pointer to a character (string):
cchar **charDoublePtr;
Here, charDoublePtr is a double pointer that can point to a char or to a pointer to a char, which is often used for handling strings.
3. Declaring a pointer to a pointer to another pointer
cint ***triplePtr;
In this example, triplePtr is a double pointer that can point to a pointer to an int or to a pointer to a pointer to an int.
How Double Pointer Works?
A double pointer (pointer to a pointer) in C works by allowing you to have a pointer that points to another pointer. This concept is used to manage levels of indirection and is especially useful for tasks like dynamic memory allocation, working with multi-dimensional arrays, and modifying pointers within functions. Let's delve into how double pointers work:
1. Memory Allocation:
When you declare a double pointer, you are essentially declaring a variable that can store the address of another pointer.
Consider the following example:
cint **doublePtr;
Here, doublePtr is a double pointer that can point to a pointer to an integer.
2. Pointer Chain:
Double pointers enable you to create a chain of pointers, where one pointer points to another, and the last pointer in the chain typically points to the actual data.
For example:
cint num = 42;int *ptr = #int **doublePtr = &ptr;
In this example, doublePtr points to ptr, which, in turn, points to num. So, by dereferencing doublePtr, you can access the value of num.
3. Modifying Pointers:
Double pointers are often used when you want to modify a pointer through a function and have those changes reflected outside the function. For instance:
cvoid modifyPointer(int **ptr) { *ptr = NULL;}
int main() {
int num = 42;
int *ptr = # int **doublePtr = &ptr;
modifyPointer(doublePtr);
// Now, ptr is NULL}
In this case, the modifyPointer function takes a double pointer as a parameter and sets the pointed-to pointer (ptr) to NULL. This change is reflected in the main function because doublePtr points to ptr.
4. Dynamic Memory Allocation:
Double pointers are commonly used when working with dynamically allocated memory. You can allocate memory for a pointer, store its address in a double pointer, and use the double pointer to manage and free the allocated memory.
cint *data = (int *)malloc(sizeof(int));int **doublePtr = &data;
// Use *doublePtr to access and manipulate the dynamically allocated memory.// Don't forget to free the memory when done.free(*doublePtr);
0 Comments