C Programming – Pointer to Pointer

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 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 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;

Statement Result Description
876 Value of the variable “i
0x6e85b06c Address of the variable “i
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 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 the same. And also, as mentioned above, **ppi can be written as * *ppi or * * ppiFor 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;

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 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?

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 pointer; that looks something like this;

pointer to pointer
pointer to pointer

// Malin

Add a Comment

Your email address will not be published.