C Programming

How Does Pointer to Pointer Work in C

Pointers are a crucial part of C programming that makes it easy for you to manipulate data and memory in a more prominent way. However, the case may arrive when you need to manipulate a pointer itself and this is where pointer to pointer will come into the business.

This article discusses the working of pointer to pointer in C programming language.

Before heading towards how pointer to pointer works, let’s first discuss what is pointer to pointer.

What is the Pointer to Pointer

A pointer is a variable used to store another variable memory address. While a pointer to pointer, also referred to as the double pointer is a pointer that is used to store the memory address of another pointer.

General Syntax to Use Pointer to Pointer in C

The following is the syntax to use a pointer to pointer in C programming.

data_type **variable_name

The pointer to pointer variable must be defined by adding two asterisks to its name. For instance, the following declaration declares a pointer to pointer of int datatype.

int **var;

How Does Pointer to Pointer Work in C

A pointer to pointer acts similarly to an ordinary pointer, except that it modifies the actual value associated with the pointer to which it points. To put it another way, the memory address held in an ordinary pointer is capable of being changed. Let’s consider a simple example:

int n = 10;
int *pptr1 = &n;
int **pptr2 = &pptr1;

In the above code, we have an int variable that stores an integer value. We also have a pointer named “pptr1” that stores the memory address of variable n. After that, the “pptr2” is used that stores the memory address of pointer pptr1.

Now, if you want to access the values of n, you just need to dereference the pointer twice. In the first dereference, you will get the memory address of “pptr1”, while in the second, you will get the value of n. Now you can get the value using the “printf()” function.

Here is a complete code to use pointer to pointer in C programming.

#include <stdio.h>

int main() {
    int n= 10;
    int *pptr1 = &n;
    int **pptr2 = &pptr1;
    printf("Value of n using pptr2: %d\n", **pptr2);
    return 0;
}

Output

We can also allocate memory for a pointer variable in a separate function and then apply that pointer in the calling function. The given example demonstrates how to do this.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void memory_allocation(char ** my_ptr)
{
    *my_ptr = (char * ) malloc(10 * sizeof(char));
}
int main()
{
    char *w;
    memory_allocation(&w);
    strcpy(w, "linuxhint");
    printf("%s\n", w);
    free(w);
    return 0;
}

In the above program, the “memory_allocation” function allocated the memory to ptr_1. The ptr_1 acts like a double pointer and stored a string named “linuxhint” which is printed on the screen.

Output

Conclusion

The pointer to pointer is a useful concept in C programming language that allows you to indirectly access a variable value via multiple pointers layers. With the help of pointer to pointer, you can manipulate a pointer by itself. The above-mentioned guidelines help you use pointer to pointer in C programming since it also includes a few basic examples that help understand the basic concept behind pointer to pointer.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.