As we discussed in, Understanding of Pointers in C, pointers are address holders. Pointers points to an address of a variable, or a function or even it can points to a 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 pointer variable, whereas in 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 the 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 below WARNING message;
warning: initialization from incompatible pointer type
Look at below code snippet, as an example of assigning an address of a pointer to 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 pointer to pointer
Dealing with data using double pointer variable, is same as dealing with data using a single pointer variable. But, most of us confuse, when we deal with double pointers. To simplify this, look at below table;
Value of the variable “i“
Address of the variable “i“
Address of the pointer variable “pi“
Address of the pointer to pointer variable “ppi“
You might have noticed, how we access the data from double pointer variable, using (**) 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 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 de-reference operator to access the data from double pointer variable. Now we will see, how to modify the data using double pointers. Below table explains this;
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 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 we need a Pointer to Pointer?
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 pointer; that looks something like this;
In my more than 30 years of experience in the IT industry, I always showed interest in learning new things; and keep practicing what I learned. In my leisure time, I read books, play games & watch movies.
I always like to play with my kids. :)
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.