🔴 Advanced  ·  Lesson 34

Pointer to Pointer

What is a pointer to pointer?

You know a pointer holds the address of a variable. A pointer to pointer (or double pointer) simply takes that one step further: it holds the address of another pointer.

So there are three things in play: the value, a pointer to that value, and a pointer to that pointer. It sounds abstract, but a quick picture makes it click.

A picture of the three levels

Memory picture
  pp   ---->   p   ---->   x
(double)   (pointer)    (value = 10)

pp   holds the address of p
p    holds the address of x
x    holds the value 10

*pp   = p   (the inner pointer)
**pp  = 10  (the final value)

Each arrow is one level of "points to". To get from pp all the way to the value, you follow two arrows — which in code means two *.

Declaring and using **

C Language
#include <stdio.h>
int main() {
    int x = 10;
    int *p = &x;      // p points to x
    int **pp = &p;    // pp points to p

    printf("x     = %d\n", x);
    printf("*p    = %d\n", *p);    // 10
    printf("**pp  = %d\n", **pp);  // 10
    return 0;
}
Output:
x = 10
*p = 10
**pp = 10

Notice **pp reaches the same 10 as *p and x — it just travels through one more level to get there.

Changing a pointer inside a function

This is the real reason double pointers matter. Because C passes by value, a function that takes a single pointer gets a copy and cannot change the caller's pointer. Passing the pointer's address (a double pointer) fixes that.

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

void allocate(int **pp) {
    *pp = malloc(sizeof(int));   // change the caller's pointer
    **pp = 99;                   // set the value
}
int main() {
    int *p = NULL;
    allocate(&p);                // pass address of p
    printf("p points to %d\n", *p);
    free(p);
    return 0;
}
Output:
p points to 99

Without the double pointer, p would stay NULL back in main. (Compare with call by value vs reference.)

Where double pointers are used

  • Functions that allocate memory for the caller and update their pointer.
  • Arrays of stringschar **argv in main is exactly this.
  • Dynamic 2D arrays — an array of pointers, each pointing to a row.
  • Linked structures — updating the head pointer of a list.

Common mistakes

  • Dereferencing the wrong number of times — mixing up *pp and **pp.
  • Passing a pointer instead of the address of a pointer when a function needs to change it.
  • Forgetting that **pp is only valid if both levels point somewhere real.
  • Getting lost in extra levels — more than two stars is rarely needed and hard to read.
🏋️ Practice

Create int x = 5;, a pointer p to it, and a double pointer pp to p. Print x, *p and **pp to confirm all three show 5, then change the value using **pp and print again.

Summary

  • A pointer to pointer stores the address of another pointer.
  • Declare it with **; dereference twice to reach the value.
  • *pp gives the inner pointer, **pp gives the final value.
  • Double pointers let a function change the caller's own pointer.
  • Used for string arrays, dynamic 2D arrays and linked structures.

Frequently Asked Questions

What is a pointer to pointer in C?
A pointer to pointer, also called a double pointer, is a pointer that stores the address of another pointer — not the address of a plain variable. It is declared with two asterisks, like int **pp, and lets you work with a pointer indirectly, one level further out.
Why would you use a double pointer?
The most common reason is to let a function change a pointer that belongs to the caller — for example allocating memory and updating the caller's pointer to point at it. Double pointers are also used for arrays of strings and for dynamically built 2D arrays.
What does ** mean in C?
A single * means "pointer to", so int * is a pointer to an int. Two of them, int **, means "pointer to a pointer to an int". When dereferencing, one * reaches the inner pointer and a second * reaches the final value.
How do you get the value from a double pointer?
You dereference it twice. If pp is a double pointer, then *pp gives the pointer it points to, and **pp gives the actual value at the end of the chain. Each * peels off one level.
Why does a function need a double pointer to change my pointer?
Because C passes arguments by value, a function receives a copy of your pointer, so changing that copy does not affect your original. Passing the address of your pointer (a double pointer) lets the function reach and modify the original pointer itself.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.