Double Pointers or Pointers to Pointers in C/C++

In this tutorial about pointers, we will discuss pointers to pointers or double pointers in C/C++. In the previous guides, we have learned some basics about pointers and how to handle them. Now, let us move a bit further today and look at a situation where pointers are a bit challenging to understand. Our aim will be to make you understand how to work with pointers to pointers in a fairly easier manner.

Pointer Basics (Memory Allocation)

Below you can see a logical horizontal view of the system’s memory. As you already know, each division allocates 1 byte of memory. The diagram shows the address associated with each byte as well. These addresses are for example purposes and do not depict the actual values. We are incrementing the address as we move from left to right.

Pointers to Pointers pic 1

Now, from the diagram above we can see that the first byte is at address 199, followed by 200, 201, 202 and so on.

For example, if we declare an integer variable called ‘x’ and store the value 10 in it.

int x = 10;

As integer data types use 4 bytes of memory space in a typical machine and each memory location can save only one byte. Hence, integer variable x is stored in four consecutive memory locations. Considering these four bytes are stored at starting address 200. This block of 4 bytes is allocated for ‘x’ and the value stored in this block is ’10.’

Pointers to Pointers pic 2

Now, we will declare a pointer variable that will store the address of ‘x.’ To do that we will have to declare a pointer to integer as shown below. An asterisk sign is used with the pointer variable declaration.

int *ptr;

This will cause some amount of memory to be reserved for the pointer variable. A pointer is also stored in 4 bytes. To store the address of ‘x’ in the variable ‘ptr’ we will use the following line of code. This way ‘ptr’ will point to ‘x’.

ptr = &x;

Considering these four bytes are stored at starting address 205. This block of 4 bytes is allocated for ‘ptr’ and the value stored in this block is the address of ‘x’. In our case it is ‘200.’

Pointers to Pointers pic 3

Remember we are able to store the address of ‘x’ in ‘ptr’ as ‘ptr’ is a pointer to an integer variable. If ‘ptr’ was a pointer to some other variable example float or string then the above line of code would not work. Both variable types should match.

Dereferencing the address

Additionally, the pointer variable is not only used to store the address of the variable but also used to dereference the address and store any value there. An example of this can be seen below.

*ptr = 6;

This will store the value ‘6’ in the variable ‘x’ instead of ’10.’ This way we dereferenced the address and stored a different in the variable.

Pointers to Pointers pic 4

Declare a Double Pointer or Pointer to a Pointer in C/C++

In the previous section, we saw how to create a pointer variable to an integer variable. Now let us learn how to create a pointer to a pointer variable or double pointer. The ‘ptr’ variable is a pointer to an integer and we want to create a pointer to ‘ptr.’ Suppose we create a variable called ‘q.’ This will store the address of ‘ptr.’ The important thing to note here is determining the data type of this variable ‘q.’ As discussed previously, we need a pointer of a particular type to store the address of a particular type of variable.

To store a pointer to integer we want a pointer to pointers. We will put two asterisks signs in front of the variable name. In our case **q. This way we will create a pointer to a pointer variable.

int **q;

To store the address of ‘ptr’ in the variable ‘q’ we will use the following line of code. This way ‘q’ will point to ‘ptr’.

int **q;
q = &ptr;

Now as we know a pointer is stored in 4 bytes hence considering these four bytes are stored at starting address 211. This block of 4 bytes is allocated for ‘q’ and the value stored in this block is the address of ‘ptr’. In our case it is ‘205.’

Pointers to Pointers pic 5

The diagram below shows the relationships between the three variables, x, ptr and q.

Pointers to Pointers pic 7

Creating Triple Pointers

Moreover, you can also create a pointer to pointer to pointer. This will be done by adding three asterisks in front of the variable name or at the end of the variable type e.g. int *** r or int ***r. Note that this variable ‘r’ will only store the address of a variable type int** thus in our case ‘q’.

int ***r;
r = &q;

To view it in the system’s memory, as an example ‘r’ got allotted at starting address 211. This block of 4 bytes is allocated for ‘r’ and the value stored in this block is the address of ‘q’. In our case it is ‘211.’ As you can see, ‘r’ points to ‘q’, ‘q’ points to ‘ptr’ and ‘ptr’ points to ‘x’

Pointers to Pointers pic 6

POINTER TO POINTER BASIC EXAMPLE IN C / C++

The below example code is the complete demonstration of all the concepts that we have learnt about pointer to pointer till now. We have complied all the individual lines of code together that we explained individually before.

#include <stdio.h>
int main()
{
int x = 10;
int *ptr = &x;
*ptr = 6;
int **q = &ptr;
int ***r = &q;
}

Double Pointers Application

In this example, we allocate memory on a heap through a function without returning the address of allocated memory through a double pointer as an argument to a function. In other words, we want to indirectly assign the address of allocated memory to a pointer. This pointer will define inside the main function.

The function allocate(int **a) allocates memory for 10 integer elements on the heap and assigns the address of the first element to local pointer p.

#include <stdio.h>
#include <stdlib.h>

void allocate(int **a){
    *a = (int*)malloc(sizeof(int)*10);
}
int main(){
    int *p = NULL;
    allocate(&p);
    for(int i=0; i<=9; i++)
    *(p+i) = i;
    for(int i=0; i<=9; i++)
    printf("%d\n", *(p+i));
    return 0;
}

Double Pointers Applications

  • Link list data structures
  • Dynamic memory allocation through a function without returing address of allocated memory
  • Dynamic arrays

Points to Note:

  • The type of variable ‘x’ is integer. Hence, to store the address of ‘x’ we will require a pointer of type int *. We will require one asterisk to denote that this is a pointer to that particular variable type.
  • Similarly, to store the address of ‘ptr’ we will require a pointer to int **. We will require two asterisks to denote that this is a pointer to a pointer variable.
  • Likewise, if we can also declare a pointer to pointer to pointer by adding three asterisk in front of the variable name. For example int ***y. This will store the address of int ** Also, r=&q will be the only valid statement in the example set we provided.

You may also like to read:

Leave a Comment