Pointer to Pointer in C

For Complete YouTube Video: Click Here

In this class, Pointer to Pointer in C.

We have already discussed the concepts of pointers.

Pointer to Pointer in C

We will try to understand the concept of Pointer to Pointer using an example.

Example

Pointer to Pointer is a pointer variable that stores the address of another pointer.

The image below is an example of Pointer to Pointer.

Pointer to Pointer in C Program
Pointer to Pointer in C Program

We will consider each line of the code and visualize every line of code.

int a = 10;

As shown below, we define an integer variable, and the compiler has assigned address 100 for a.

Pointer to Pointer in C
Pointer to Pointer in C

int *p = &a;

Here p is an integer pointer variable storing the address a.

The pointer p is assigned to address 200 by the compiler, as shown below.

Pointer to Pointer in C Visualization 2
Pointer to Pointer in C Visualization 2

int **ptp = &p;

The above statement is the definition of a Pointer to Pointer.

Here ‘ptp’ is a pointer that stores another ‘pointer’ address, which points to an integer variable.

The compiler has assigned ptp at 300 memory locations, as shown below.

Pointer to Pointer in C Visualization 3
Pointer to Pointer in C Visualization 3

printf(“a = %d\n”, a);

The above printf will print the value stored in a, 10.

printf(“*p = %d\n”, *p);

The above printf will print the value stored in the address p is pointing to, 10.

printf(“**ptp = %d\n”, **ptp);

The above **ptp has two indirection operators.

The simplified way of representing the above expression is (ptp).

(*ptp) is 100, and *(100) is 10.