C – Pointer to Pointer (Double Pointer)

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Similar to how a pointer variable in C can be used to access or modify the value of a variable in C, a pointer to pointer in C is used to access/modify the value of a pointer variable. Here, the "value" of the former pointer is as usual a memory address. So, using a pointer to the pointer aka a double pointer in C, we can make the previous pointer point to another memory location.

Prerequisite

Double Pointer Illustration

pointer to pointer in C

  • In essence, a double pointer in C holds the memory address of another pointer. This means that it provides an indirect reference to the variable the first pointer points to. Imagine it as a chain where the double pointer points to another pointer, which in turn points to the actual variable.

Declaration of Pointer to Pointer in C

  • To declare a double pointer, you utilize two asterisks before the variable name. Syntax: pointer_data_type **variable_name; For example:
  • Trying to make a double pointer directly point to a normal variable, like with int **double_ptr = &var;, will lead to a compiler warning. The correct approach is to point to a regular pointer.

Example of Double Pointer in C

A double pointer, also known as a pointer to a pointer, is a powerful concept in C programming. It allows you to indirectly access and modify variables through multiple levels of indirection. Here's a basic example to illustrate the usage of double pointers:

In this example:

  • We declare an integer variable value and initialize it with the value 42.
  • We declare a pointer ptr1 and make it point to the address of value.
  • We declare a double pointer ptr2 and make it point to the address of ptr1.

We then demonstrate how to access and modify the value of value using all three levels of indirection: directly using value, through ptr1, and through ptr2. Finally, we update the value through ptr2 and verify the change.

This example showcases the concept of double pointers and their ability to indirectly access and modify variables, making them a valuable tool in C programming.

How Double Pointer Works?

A double pointer, often referred to as a pointer to a pointer, is essentially a variable that stores the address of another pointer. In the C programming language, pointers are variables that hold memory addresses, typically of other variables. A double pointer takes this concept one level deeper.

Illustration

Imagine you have a box (var) that contains a value. Now, there's another box (ptr) that doesn't hold a direct value but rather a paper that tells you where the first box is located. This second box is a pointer. Now, imagine a third box (double_ptr), which instead of having a paper pointing to a direct value, has a paper pointing to the second box (ptr). This is the concept of a double pointer.

Explanation

Let's break it down step-by-step with code and explanation:

  1. Regular Variable Declaration:

    Here, an integer variable var is declared and assigned the value 10.

  2. Pointer Declaration:

    An integer pointer ptr is declared, and it is assigned the address of the var variable.

  3. Double Pointer Declaration:

    Here, a double pointer double_ptr is declared, and it's assigned the address of the ptr pointer.

With this setup:

  • *ptr will give the value 10 (value of var).
  • *double_ptr will give the address of var (because it points to ptr which holds the address of var).
  • **double_ptr will give the value 10 (value of var).

In essence, a double pointer provides an indirect way to access the value of the original variable. This level of indirection becomes useful, especially when dealing with dynamic data structures like linked lists, trees, and 2D arrays.

Size of Pointer to Pointer in C

  • Size is a key aspect to understand. While the data a pointer or double pointer refers to can be of various sizes, the pointers themselves typically have a fixed size, determined by the system architecture.
  • On a 32-bit system, a pointer generally occupies 4 bytes, while on a 64-bit system, it's typically 8 bytes. This size remains consistent regardless of whether it's a single or double pointer.

Example 1: Program to find the size of a pointer to a pointer in C

Application of Double Pointers in C

  • Storing a list of strings: Double pointers can be used to create a dynamic array of strings, offering a space-efficient alternative to 2D arrays.
  • Dynamic memory allocation: Especially useful when dealing with 2D arrays. Instead of fixed-size arrays, using double pointers allows for flexible dimensions.
  • Command-line arguments: The main function in C accepts a double pointer, argv, which is an array of strings passed from the command line.

Multilevel Pointers in C

Multilevel pointers provide multiple layers of indirection in C. While double pointers are often used for applications like dynamic 2D arrays or arrays of strings, as we move beyond that, each additional level of indirection provides more flexibility but also introduces more complexity.

For instance:

  • A single pointer (*ptr) points to a variable.
  • A double pointer (**ptr) points to a single pointer, which in turn points to a variable.
  • A triple pointer (***ptr) points to a double pointer, which points to a single pointer, eventually leading to the variable.

And the pattern can continue, though, in practical applications, you'll rarely see pointers with more than two levels of indirection.

Syntax of Triple Pointer in C

The declaration of a triple pointer involves three asterisks before the variable name. Here's how you can declare and use a triple pointer:

Output:

In the above example, ***triple_ptr provides three levels of indirection to access the value of var. First, it accesses the memory location of double_ptr, then ptr, and finally var.

Conclusion

  • Double pointers in C are very powerful and can have many applications (as explained in the Examples section) apart from simple data manipulation.
  • In most cases, it is a personal preference whether to make use of a double pointer or use a workaround. However, in certain scenarios, the use of double pointers becomes mandatory. One such example is if we want to store a list of variable sized strings in a space efficient manner or if the size of a 2D array can change during the course of program execution.
  • To change the value of a double pointer, we can use a "triple" pointer, which is a pointer to a pointer to a pointer (eg. int ***triple_ptr). Similarly, to change the value of a triple pointer we can use a pointer to a pointer to a pointer to a pointer. In other words, to change the value of a "Level X" variable, we can use a "Level X+1" pointer. Thus, this concept can be extended to further levels.