Course contentsShow
← Computer Science
Lesson 319 of 2,8728. Linked Data StructuresFree lesson

Pointers to Pointers (Double Indirection)

Understanding multi-level pointers where a pointer stores the address of another pointer variable.

Pointers to Pointers (Double Indirection)

What You'll Learn

You'll understand how a pointer can store the address of another pointer variable, creating a chain of indirection.

The Core Idea

Remember that a pointer is just a variable that holds a memory address. Well, since a pointer is a variable, it has its own address in memory too! A pointer to a pointer stores the address of another pointer variable.

Think of it like a treasure map that leads to another map, which then leads to the treasure. You follow the first map to find the second map, then follow the second map to find the actual treasure.

In code, we declare a pointer to a pointer using two asterisks (**):

int value = 42;
int *ptr = &value;      // ptr points to value
int **ptrToPtr = &ptr;  // ptrToPtr points to ptr

Here's what's happening:

  • value holds the number 42
  • ptr holds the address of value
  • ptrToPtr holds the address of ptr

To access the final value through double indirection, you dereference twice:

int result = **ptrToPtr;  // result is 42

The first * dereferences ptrToPtr to get ptr, and the second * dereferences ptr to get value.

Why Use This?

Double pointers are essential when you need to modify a pointer itself (not just the data it points to) inside a function. They're also used in dynamic arrays of pointers and linked data structures where you need to change what a pointer points to.

Key Takeaway: A pointer to a pointer (**) creates a chain of addresses—it stores the address of another pointer, requiring two dereferences to reach the final value.