What is pointer to pointer in C?

In case of a pointer to pointer concept, one pointer refers to the address of another pointer. The pointer to pointer is a chain of pointers. Generally, the pointer contains the address of a variable. The pointer to pointer contains the address of a first pointer.

In C, a pointer to a pointer is a concept that allows you to have a pointer variable that holds the address of another pointer variable. This means you have two levels of indirection.

Here’s a brief explanation:

In C, a pointer variable holds the memory address of another variable. So, if you have a pointer int *ptr, it can hold the address of an integer variable. Now, if you have a pointer to a pointer, say int **ptr_ptr, it can hold the address of another pointer variable.

Here’s an example to illustrate:

c
#include <stdio.h>

int main() {
int num = 10;
int *ptr = &num; // Pointer to an integer

int **ptr_ptr = &ptr; // Pointer to a pointer to an integer

printf("Value of num: %d\n", num);
printf("Value of num using single pointer: %d\n", *ptr);
printf("Value of num using double pointer: %d\n", **ptr_ptr);

return 0;
}

In this example, ptr_ptr is a pointer to a pointer. It holds the address of ptr, which itself holds the address of num. So, by dereferencing ptr_ptr twice (**ptr_ptr), we can access the value of num.

This concept is often used in scenarios where you want to modify a pointer variable within a function and have those changes reflected outside the function. By passing a pointer to a pointer to the function, you can modify the original pointer directly.