As we discussed in, Understanding of Pointers in C, pointers are address holders. Pointers point to an address of a variable, or a function or even it can point to another pointer. Through this article, we are going to discuss Pointer to Pointer concepts in C.
A pointer to pointer points to an address of a pointer.
Define Pointer to Pointer variable
Two asterisk symbols (**) are used to define a pointer to a pointer variable, whereas in the pointer variable we use a single asterisk (*) symbol. Here is an example;
int **ppi = 0;
It is not required to keep (**) together when dealing with double pointers. But for readability, it is recommended to use together. Below statements are also valid;
int * * ppi = 0;
int * *ppi = 0;
Once define the variable, we need to assign an address of a pointer. If we assign an address of a variable; we will get the below WARNING message;
warning: initialization from incompatible pointer type
Look at the below code snippet, as an example of assigning an address of a pointer to a double-pointer variable;
int i = 876;
int *pi = &i;
int **ppi = π
Notice that, address-of (&) operator is used to get an address of the pointer variable; like, we use this operator to get an address of a variable.
Accessing and modifying data using a pointer to pointer
Dealing with data using a double pointer variable is the same as dealing with data using a single pointer variable. But, most of us are confused, when we deal with double pointers. To simplify this, look at the below table;
Statement | Result | Description |
i | 876 | Value of the variable “i “ |
&i | 0x6e85b06c | Address of the variable “i “ |
&pi | 0x6e85b070 | Address of the pointer variable “pi “ |
&ppi | 0x6e85b078 | Address of the pointer to pointer variable “ppi “ |
You might have noticed, how we access the data from a double-pointer variable, using the (**) symbol. It is not a de-reference operator; the de-reference operator is (*). The statement **ppi
is actually read it as *(*ppi)
, for better readability.
**ppi
and *(*ppi)
are the same. And also, as mentioned above, **ppi
can be written as * *ppi
or * * ppi
. For better readability, stick to the format and naming convention, guidelines.
I hope now you understood how we use the dereference operator to access the data from the double-pointer variable. Now we will see, how to modify the data using double pointers. The below table explains this;
Statement | Result |
i = 278; | Value of the variable “i ” is 278 |
*pi = 345; | Now, the value of the variable “i ” is 345 |
**ppi = 276; | Again, the value of “i ” modified with the value 276 |
Remember that, *ppi
gives the address of the pointer variable pi
, not the value of the variable “i
“.
All above statements are used to modify the value stored in the variable “i
“. As the pointers are points to the address of this variable, we have successfully modified the data in “i
“.
Why do we need a Pointer to Pointer?
You might be wondering why we need to use double pointers when we can do most of the things with single pointers. Yes, we can use single pointers also. But, when we deal with multi-dimensional data, usually we use double pointers. Also, these are mostly used in complex data structures, image processing, data analysis, etc,.
Finally, when we draw a picture of pointer to a pointer; that looks something like this;
// Malin