C – Pointer to Pointer (Double Pointer)
Prerequisite: Pointers in C and C++
We already know that a pointer points to a location in memory and is thus used to store the address of variables. So, when we define a pointer to a pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to change the values of normal pointers or create a variable-sized 2-D array. A double pointer occupies the same amount of space in the memory stack as a normal pointer.
How to Declare a Pointer to a Pointer in C?
Declaring Pointer to Pointer is similar to declaring a pointer in C. The difference is we have to place an additional ‘*’ before the name of the pointer.
data_type_of_pointer **name_of_variable = & normal_pointer_variable;
int val = 5; int *ptr = &val; // storing address of val to pointer ptr. int **d_ptr = &ptr; // pointer to a pointer declared // which is pointing to an integer.
The below diagram explains the concept of Double Pointers:
The above diagram shows the memory representation of a pointer to a pointer. The first pointer ptr1 stores the address of the variable and the second pointer ptr2 stores the address of the first pointer.
Let us understand this more clearly with the help of the below program:
Value of var = 789 Value of var using single pointer = 789 Value of var using double pointer = 789
What will be the size of a pointer to a pointer in C?
In the C programming language double pointer behave similarly to a normal pointer in C. So, the size of the double-pointer variable and the size of the normal pointer variable is always equal.
Size of normal Pointer: 8 Size of Double Pointer: 8
Note: The output of the above code also depends on the type of machine which is being used. The size of a pointer is not fixed in the C programming language and it totally depends on other factors like CPU architecture and OS used. Usually, for a 64-bit Operating System, the size will be 8 bytes and for a 32-bit Operating system, the size will be 4 bytes.
What if we want to change the value of a double pointer?
In this case, we can use a triple pointer, which will be a pointer to a pointer to a pointer i.e, int ***t_ptr. And similarly to change the value of a triple pointer we can use a pointer to a pointer to a pointer to a pointer. In other words, we can say that to change the value of a ” level – x ” variable we can use a ” level – x+1 ” pointer. And this concept can be extended further.
Must Read – Function Pointer in C
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Please Login to comment...