Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 135 ยท Pointers

Pointers to Pointers

Learn C double pointers - what int ** means, how a function changes the caller's pointer, arrays of strings, argv, and row-by-row 2D arrays with malloc.

A pointer is a variable, and every variable lives somewhere in memory. So a pointer has an address too, and you can store that address in another pointer. A pointer to a pointer (often called a double pointer) is written with two stars: if p is an int *, then &p is an int **.

Two stars look intimidating, but double pointers show up for two practical reasons. First, they let a function change the caller's pointer, not just the value it points to. That's how strtol reports where it stopped and how allocation helpers hand back new memory. Second, they describe arrays of pointers, most importantly arrays of strings like the argv parameter of main. This lesson covers both, plus row-by-row two-dimensional arrays and the difference between char ** and a real 2D array.

Make sure Pointers and Pointers and Arrays feel comfortable first.

Two levels of indirection

Each * in a declaration adds one level. Each * in an expression follows one level. With int **pp = &p; and int *p = &x;:

  • pp is the address of p.
  • *pp is p itself, which is the address of x.
  • **pp is x.
C Following two pointers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The last two assignments are the whole point. **pp = 42 writes through both pointers and changes x. *pp = &y writes through only one level, so it changes p and makes it point somewhere else. Through a double pointer you can modify either the final value or the pointer in the middle.

The types guard each step. pp is int **, *pp is int *, **pp is int. If you try int **pp = &x;, the compiler stops you, because &x is an int *, one level short. Reading the declaration right to left helps: int **pp is "pp is a pointer to a pointer to an int".

Changing the caller's pointer

C passes every argument by value. When you pass a pointer to a function, the function gets a copy of the address. It can change what that address points to, but assigning a new address to its parameter changes only its own copy. To change the caller's pointer, the function needs the pointer's address: a pointer to a pointer.

C Why a function needs char **

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

skip_spaces_wrong does advance its cursor, but that's a local copy that vanishes when the function returns, so text is unchanged. skip_spaces receives &text, so *cursor is text, and (*cursor)++ moves the caller's pointer.

Watch the parentheses in (*cursor)++. Without them, *cursor++ would mean *(cursor++): it would move the local double pointer, not the string pointer, which is a bug that compiles silently (with a "value computed is not used" warning, if you're lucky).

This is exactly the design of strtol(text, &end, 10) from Converting Strings and Numbers. Its second parameter is a char **: you pass the address of your end pointer, and strtol stores the stop position there.

Returning new memory through a parameter

The same idea is how a function can allocate memory and also return an error code. The return value says whether it worked; the new pointer comes back through a ** parameter. (Allocation with malloc is covered fully in Dynamic Memory.)

C An out-parameter for allocated memory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The caller starts with name = NULL, passes &name, and after a successful call name points to fresh memory. The function writes the result with *out = copy;, one level of dereference, because it's changing a pointer, not a character. A matching cleanup function often takes a ** too, so it can free the memory and set the caller's pointer to NULL in one step: void release(char **p) { free(*p); *p = NULL; }.

Arrays of strings

An array of strings in C is usually an array of pointers, each pointing to the first character of a string. Its type is const char *names[], and when it decays, as every array does, it becomes a const char **. That's the second big reason double pointers exist.

C An array of string pointers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

A few things are worth noticing:

  • sizeof planets is 32: four pointers of 8 bytes each. The characters themselves are stored elsewhere, in the string literals.
  • planets[2][0] applies two subscripts: planets[2] is the pointer to "Earth", and [0] is its first character, 'E'.
  • Reordering means swapping pointers, which is fast no matter how long the strings are. Sorting arrays of strings with qsort works the same way, as you'll see in qsort and bsearch.
  • A NULL sentinel at the end is a common alternative to passing a count. count_until_null walks the array with a const char *const * pointer and stops at NULL. That parameter type means "pointer to read-only pointers to read-only characters"; const and Pointers explains how to read it.

Pointer arrays versus 2D char arrays

There's another way to store several strings: a two-dimensional char array, where every row has the same fixed width.

C
const char *as_pointers[] = {"Mercury", "Venus", "Earth"};   /* 3 pointers, strings elsewhere */
char as_grid[][8] = {"Mercury", "Venus", "Earth"};          /* 3 rows of 8 chars, 24 bytes */

The pointer version uses exactly as much space as each string needs (plus a pointer each), and the strings are read-only literals. The grid version reserves 8 bytes per row whether the word needs them or not, but the characters are a writable copy inside the array. The types are also different: as_pointers decays to const char **, while as_grid decays to char (*)[8]. You can't pass one where the other is expected.

argv: the command line as char **

You've been writing int main(void), but main has a second standard form:

C
int main(int argc, char *argv[])    /* or: int main(int argc, char **argv) */

When a program starts, the operating system passes it the command-line words. argc is how many there are and argv is an array of pointers to them, with argv[0] usually holding the program's name and argv[argc] guaranteed to be NULL. As a parameter, char *argv[] is adjusted to char **argv, exactly like any other array parameter.

Our editor runs programs without command-line arguments, so the example below builds an argv-style array by hand and passes it to a function shaped like main. That's also a handy way to test argument parsing in your own projects.

C Simulating argc and argv

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

If you ran the real program as ./report -v sales.csv 2026, the operating system would create the same structure: argc is 4, and argv points to an array of five pointers whose last one is NULL. Each argv[i] is a char * you can read character by character, which is why argv[i][0] works. Command-Line Arguments builds a full option parser on top of this.

(main's argv uses plain char * for historical reasons; the strings are actually writable. In the simulation the strings are literals, so run must not modify them.)

Row-by-row 2D arrays with int **

When a table's size is known only at run time, a common approach is an array of row pointers, each pointing to its own block of memory. The rows can even have different lengths (a jagged array). This layout is what int ** really describes:

C A jagged array with int **

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

rows points to an array of int *, and each rows[r] points to its own array of int. The expression rows[r][c] follows two pointers: rows[r] reads a row pointer from the first array, then [c] indexes into that row. Compare that with a true 2D array int grid[4][4], where all 16 numbers sit in one block and grid[r][c] is computed with a single multiplication. Both use the same [r][c] syntax, which is why it's easy to forget they're laid out completely differently.

Notice the sizeof *rows and sizeof *rows[r] idiom: *rows is an int * and *rows[r] is an int, so the sizes stay correct even if you later change the element type. Freeing happens in reverse: every row first, then the array of row pointers.

Tip: If every row has the same length, a single allocation is simpler and faster: int *cells = malloc(rows * cols * sizeof *cells); and index with cells[r * cols + c]. Use int ** when rows really differ in length or must be swapped cheaply. Building a Dynamic Array and Multidimensional Arrays compare the options.

How many stars is too many?

int *** is legal, and you'll occasionally see it, for example a function that returns a newly allocated int ** table through an out-parameter. Beyond that, extra levels usually mean the design needs a struct. In practice you'll meet ** in three places: out-parameters that change a pointer, arrays of pointers (strings, argv), and linked-data code that keeps a pointer to "the link to change", which you'll see in Linked Lists.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Given int x = 5; int *p = &x; int **pp = &p;, which expression has type int * and equals &x?

    *pp follows one level and gives p, which holds &x. **pp is x itself, pp is &p, and &pp would be an int ***.
  2. 2. A function should make the caller's char *buffer point to newly allocated memory. Which parameter type does it need?

    To change the caller's pointer, the function needs that pointer's address, a char **. It then assigns *out = new_memory;.
  3. 3. Inside void advance(const char **cursor), which statement moves the caller's string pointer one character forward?

    (*cursor)++ increments the pointer that cursor points to, which is the caller's pointer. *cursor++ parses as *(cursor++) and only moves the local parameter.
  4. 4. On a 64-bit system, what is sizeof names for const char *names[] = {"a", "bb", "ccc"};?

    names is an array of 3 pointers, and each pointer is 8 bytes. The characters live in the string literals, not in the array.
  5. 5. Which statement about argv is guaranteed by the C standard?

    The standard guarantees argv[argc] == NULL. argv[0] is usually the program name but may be empty, argc can be 1 (or even 0), and argv is an array of pointers, not a fixed-width grid.

Summary

  • A pointer to a pointer stores the address of a pointer: int **pp = &p;. *pp is p, **pp is the final value.
  • Arguments are copies, so a function that must change the caller's pointer takes its address (char **) and assigns through it: *out = ... or (*cursor)++.
  • strtol's char **end and allocation helpers that return an error code both use this pattern.
  • An array of strings is usually an array of pointers (const char *items[]), which decays to const char **. A NULL sentinel can mark its end.
  • main(int argc, char *argv[]) receives the command line as such an array, with argv[argc] == NULL; you can simulate it by building the array yourself.
  • int ** describes row pointers to separately allocated rows (possibly jagged), which is a different layout from a true 2D array int g[R][C].
  • Free nested allocations from the inside out: rows first, then the array of row pointers.

Next: const and Pointers