Double Pointers in C/C++

Noah11012 - Jan 12 '19 - - Dev Community

One source of confusion among new C programmers is pointers. At first glance, there doesn't seem to be much usefulness in using them. But, it is crucial to understand pointers as it is a useful tool and any project bigger than a "Hello, World" program will have pointers. Once beginners start to grasp the concept and applicability of pointers, another wave of terror strikes deep into their hearts: double pointers.

Before moving on any further, let's recap what a pointer is.

In C and other languages like C++, a pointer is something that holds the memory address of an object. They are a numeric value and when outputted to the console they are usually presented in hexadecimal. This means, essentially, pointers are just fancy integers.

Now back to double pointers.

Upon seeing a double pointer, a beginner begins to shift uncomfortably, sweat running down their forehead all the way to their chin. Like everything else in life that is new or foreign, people feel awkward and uneasy even just slightly at the prospect of facing something they have never encountered before. But as you learn more, you start to feel confident in your new understanding of anything you desired to conquer. The same goes with double pointers.

So, what is a double pointer? Well, if a regular pointer is to refer to an object in memory, then a double pointer is a variable that points to another pointer which in turn, points to an object in memory.

Examples:

#include <stdio.h>

int main(void)
{
    int value = 100;
    int *value_ptr = &value;
    int **value_double_ptr = &value_ptr;

    printf("Value: %d\n", value);
    printf("Pointer to value: %d\n", *value_ptr);
    printf("Double pointer to value: %d\n", **value_double_ptr);
}
Enter fullscreen mode Exit fullscreen mode

Output:

~/Desktop 
 clang main.c

~/Desktop 
 ./a.out     
Value: 100
Pointer to value: 100
Doublue pointer to value: 100

~/Desktop 
 
Enter fullscreen mode Exit fullscreen mode

When dereferencing a double pointer, we do not get the final object, but what is expected: a pointer that must be dereferenced one more time to retrieve the final value. It would be similar to the code below.

int *ptr = *value_double_ptr;
int final_value = *ptr;
Enter fullscreen mode Exit fullscreen mode

Now I'm sure that most who are new to double pointers must be asking themselves the question, "Where will I ever use this?". Probably the best way to present the usefulness of double pointers is to remember one of the practical uses of regular pointers. This is to say, regular pointers can be used as an output parameter in functions. An output parameter, if you happen to not know, is a parameter that is filled with a result. Why you would use output parameters is subject to several factors and is beyond the scope of this article but I felt this is where double pointers can be utilized.

A function that we will take a look at is the POSIX getline() function. Its purpose is to read one line from a file. When the line is read, one might suspect it to be returned, but, this is not the case as the return value is used for something different. Instead, the first argument, which takes a double pointer, is filled with the buffer that contains the line.

Why does it take a double pointer? So, that it can allocate enough memory to hold the entire line plus the null terminator character. If memory allocation and reading the one line is successful, then the pointer supplied is modified to point to the new and filled buffer. They could have only achieved this if they were using a double pointer.

When passing anything to a function, a copy is passed and not the actual object. The same goes for a pointer. If we passed a regular pointer, we could only modify the contents that the pointer points to. If we changed the pointer itself, the change would not reflect outside of the function because it is a copy.

If we want to change where the pointer points to, we have to add another indirection and take a double pointer.

An example of using getline() might be the following.

#include <stdio.h> 

int main(void)
{   
    FILE *file = fopen("test", "r");

    char *line = 0;
    int   line_length = 0;
    getline(&line, &line_length, file);

    printf("%s\n", line);
    printf("Line length: %d\n", line_length);
}
Enter fullscreen mode Exit fullscreen mode

getline() has two output parameter: one for the buffer and the other for the length of the line that was read in.

Summary

Double pointers are a challenge to many beginners immediately when first learned. As you become more comfortable with the idea of double pointers and use them when necessary in your own code, you start to think how silly that once you were afraid of double pointers.

And now I send you off with your new knowlegde!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player