Skip to content
StudyFame

Double Pointers in C

A pointer is a variable, so it also lives somewhere in memory and has its own address. A pointer to pointer stores that address. It is also called a double pointer, written with two stars: int **pp.

This sounds confusing at first, but one diagram makes it clear. In this lesson you will declare double pointers, use **pp, and see the main real use: letting a function change the caller's pointer.

What You Will Learn

  • What a pointer to pointer is.
  • How to declare and initialize it.
  • What pp, *pp and **pp mean.
  • How a function can change a pointer in main.
  • Where double pointers are used.

What is a Pointer to Pointer (Double Pointer)?

Exam definition: A pointer to pointer (double pointer) is a pointer that stores the address of another pointer. It is declared with two asterisks, e.g. int **pp;, and **pp gives the value of the original variable (two levels of indirection).

Memory trick: A treasure hunt with two clues. Clue 1 (pp) tells you where clue 2 is hidden. Clue 2 (p) tells you where the treasure (x) is. Each star = open one clue.

int x = 50;
int *p = &x;
int **pp = &p;

    pp                  p                  x
 +--------+         +--------+         +--------+
 |  3000  | ------> |  2000  | ------> |   50   |
 +--------+         +--------+         +--------+
 addr 4000          addr 3000          addr 2000

pp   = 3000  (address of p)
*pp  = 2000  (value of p = address of x)
**pp = 50    (value of x)

Syntax of a Double Pointer

int x = 50;
int *p = &x;       /* p  : pointer to int          */
int **pp = &p;     /* pp : pointer to pointer to int */

**pp = 60;         /* changes x */
*pp = &y;          /* changes p: now p points to y */
ExpressionTypeMeaning
ppint **Address of p
*ppint *The pointer p itself (address of x)
**ppintThe value of x

Example 1: Reaching a Variable Through Two Pointers

#include <stdio.h>

int main(void)
{
    int x = 50, y = 75;
    int *p = &x;
    int **pp = &p;

    printf("x = %d, *p = %d, **pp = %d\n", x, *p, **pp);
    printf("*pp == p ? %s,  p == &x ? %s\n",
           *pp == p ? "yes" : "no", p == &x ? "yes" : "no");

    **pp = 60;                      /* change x through two levels */
    printf("after **pp = 60: x = %d\n", x);

    *pp = &y;                       /* change p itself */
    printf("after *pp = &y : *p = %d, **pp = %d\n", *p, **pp);

    return 0;
}

Output

x = 50, *p = 50, **pp = 50
*pp == p ? yes,  p == &x ? yes
after **pp = 60: x = 60
after *pp = &y : *p = 75, **pp = 75

Code Explanation

  1. int **pp = &p; stores the address of the pointer p. The type must be int ** because p is an int *.
  2. **pp opens two levels: pp → p → x, so it reads 50.
  3. **pp = 60; writes into x.
  4. *pp = &y; writes into p: now p points to y. That's the special power of a double pointer: it can change where a pointer points.

How a Function Changes the Caller's Pointer

Remember: to change an int in main, a function needs an int *. In the same way, to change an int * in main, the function needs an int **.

To change a…PassParameter type
int x&xint *
int *p&pint **

Example 2: A Function That Moves the Caller's Pointer

The function below makes main's pointer point to the smallest element. With a single pointer parameter, the change would be lost.

#include <stdio.h>

void point_to_min_wrong(int *arr, int n, int *p)    /* p is a copy */
{
    int i;
    for (i = 0; i < n; i++)
        if (arr[i] < *p)
            p = &arr[i];        /* changes only the local copy */
}

void point_to_min(int *arr, int n, int **pp)        /* address of main's pointer */
{
    int i;
    *pp = &arr[0];
    for (i = 1; i < n; i++)
        if (arr[i] < **pp)
            *pp = &arr[i];      /* changes main's pointer */
}

int main(void)
{
    int a[] = {40, 15, 72, 9, 33};
    int *ptr = &a[0];

    point_to_min_wrong(a, 5, ptr);
    printf("Wrong way : ptr points to %d\n", *ptr);

    point_to_min(a, 5, &ptr);
    printf("Right way : ptr points to %d (index %d)\n", *ptr, (int)(ptr - a));

    return 0;
}

Output

Wrong way : ptr points to 40
Right way : ptr points to 9 (index 3)
  • In the wrong version, p is a copy of ptr. Moving the copy doesn't move ptr.
  • In the right version, we pass &ptr. Writing *pp = ... changes ptr in main.
  • This is exactly why functions that allocate memory for you (Module 12) or insert at the head of a linked list (Module 16) take a double pointer.

Where Double Pointers are Used

  • Changing a caller's pointer inside a function (as above).
  • Dynamic 2D arrays: int **matrix built with malloc.
  • Array of strings: char **, as in int main(int argc, char **argv) for command-line arguments.
  • Linked lists: inserting or deleting the first node with struct node **head.

Important Points to Remember

  • A double pointer stores the address of a pointer.
  • Declare with two stars: int **pp = &p;
  • *pp = the pointer; **pp = the final value.
  • To change a pointer in a function, pass &pointer to an int ** parameter.
  • Levels can go deeper (int ***), but more than two is rare.
  • All levels have the same pointer size (8 bytes on 64-bit).

Common Mistakes Beginners Make

MistakeWhat happensFix
int **pp = &x; (x is int)Type warning, wrong valuesint **pp = &p; where p is int *
Using *pp when you want the valuePrints an addressUse **pp
Passing ptr instead of &ptr to an int **Warning, crashPass &ptr
Changing the copy p = ... in a functionCaller's pointer unchangedTake int ** and write *pp = ...
Using int ** for a normal 2D array int m[3][4]Wrong type, crashUse int (*p)[4]

Pointer to Pointer Practice Questions

  1. Define a double pointer.
  2. If int a = 9, *p = &a, **q = &p;, what are *q == p and **q?
  3. Which statement changes a using q?
  4. Why does a function need an int ** to change a caller's int *?
  5. Give two uses of double pointers.
Show answers
  1. A pointer that stores the address of another pointer.
  2. True (1), and 9.
  3. **q = new_value;
  4. Arguments are copies; to modify the pointer variable itself, the function needs its address, which has type int **.
  5. Changing a caller's pointer (e.g. linked list head), dynamic 2D arrays, char **argv.

Coding Exercise

Write void swap_pointers(int **a, int **b) that swaps where two pointers point (not the values). Show that after the call, p1 points to y and p2 to x, while x and y are unchanged.

Show solution
#include <stdio.h>

void swap_pointers(int **a, int **b)
{
    int *temp = *a;
    *a = *b;
    *b = temp;
}

int main(void)
{
    int x = 1, y = 2;
    int *p1 = &x, *p2 = &y;

    swap_pointers(&p1, &p2);
    printf("*p1 = %d, *p2 = %d\n", *p1, *p2);
    printf("x = %d, y = %d\n", x, y);
    return 0;
}
*p1 = 2, *p2 = 1
x = 1, y = 2

Quick Quiz: Pointer to Pointer (MCQ)

  1. A double pointer stores the address of:

    A) An int   B) A pointer   C) A function   D) An array only

  2. Correct declaration for the address of int *p:

    A) int *q = &p;   B) int **q = &p;   C) int **q = p;   D) int &q = p;

  3. int x = 4, *p = &x, **q = &p; **q += 3; Now x is:

    A) 4   B) 3   C) 7   D) Address

  4. *q in the above code is:

    A) x   B) p (address of x)   C) Address of q   D) 7

  5. In int main(int argc, char **argv), argv is a:

    A) Pointer to char   B) Pointer to pointer to char   C) int   D) Function

Show answers
  1. B: a pointer.
  2. B: int **q = &p;
  3. C: 7.
  4. B: p (the address of x).
  5. B: pointer to pointer to char.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Double pointerStores the address of a pointer
Declarationint **pp = &p;
*ppThe pointer p
**ppThe value of x
Change caller's pointerPass &ptr to int **
Common usesargv, linked list head, dynamic 2D arrays

Frequently asked questions

What is a pointer to pointer?
A pointer that holds the address of another pointer, declared with two asterisks.
Why do we need double pointers?
Mainly so a function can change a pointer in the caller (and for arrays of strings / dynamic 2D arrays).
What is the difference between *pp and **pp?
*pp is the inner pointer (an address); **pp is the final value.

Remember

  • Treasure hunt: each star opens one clue.
  • Change an int → int *; change an int * → int **.

Next Topic

Next lesson: Array of Pointers in C. You will store many addresses in one array, the array of pointers.