CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C Programming – Pointer to Pointer

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;

StatementResultDescription
i
*pi
**ppi
*(*ppi)
876Value of the variable “i
&i
pi
*ppi
0x6e85b06cAddress of the variable “i
&pi
ppi
0x6e85b070Address of the pointer variable “pi
&ppi0x6e85b078Address 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 * * ppiFor 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;

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

pointer to pointer
pointer to pointer

// Malin

C Programming – Pointer to Pointer

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top