A pointer to a pointer in C, also called a double pointer, is a variable that stores the address of another pointer. You declare it with two asterisks, for example int **pp, and you reach the final value by dereferencing twice with **pp. A double pointer in C is mainly used when a function must change a pointer that belongs to the caller, and when working with arrays of pointers such as lists of strings.
The idea follows directly from ordinary pointers: a pointer is a variable, so it lives at an address, and that address can be stored in another pointer.
Each level of * adds one level of indirection. Start with an int, then a pointer to it, then a pointer to that pointer:
The addresses will differ on your machine. The key reading rules are:
| Expression | Type | Meaning |
|---|---|---|
pp | int ** | Address of the pointer p |
*pp | int * | The pointer p itself |
**pp | int | The integer value |
C passes arguments by value. If a function receives an int * and assigns a new address to it, only its local copy changes. To let the function change the caller's pointer, pass the address of that pointer, which is an int **.
The classic case is allocation inside a helper function:
Inside allocate_array, *out = malloc(...) writes the new address into the caller's data. If the parameter were a plain int *, the caller's data would remain NULL and the memory would leak. The parentheses in (*out)[i] are required because [] binds tighter than *.
A second common use is an array of pointers. Each element is a pointer to the first character of a string, and the array name decays to a pointer to its first element, which has type char **. You have already seen this in main:
argv is a char **: it points to an array of char *, each pointing to one command-line argument. The same shape is used by lists of names, tables of messages and environment variables.
A T ** can also represent a table whose rows are allocated separately. This gives rows of independent lengths and the familiar grid[r][c] syntax:
You must free every row first and then the array of row pointers. Note that this layout is not one contiguous block, so it is not the same type as int grid[3][4], and you cannot pass one where the other is expected.
data instead of &data to a function that expects int ** is a type error, and ignoring the warning leads to crashes.**pp is invalid if p is NULL or uninitialised. Check *pp before the second dereference.const char ** with char **: C does not convert between them implicitly, so a function taking const char ** cannot be called with a plain char ** without a cast.int **pp mean in C?It declares pp as a pointer to a pointer to int. The variable pp holds the address of an int *, and **pp is the int at the end of the chain.
Use it when a function must modify a pointer owned by the caller, such as allocating memory or advancing a list head, and when you work with arrays of pointers like char **argv.
No. A 2D array such as int a[3][4] is one contiguous block. An int ** points to separate row allocations, so the two types are not interchangeable.
The C standard guarantees support for a reasonable depth, and compilers accept far more than you will ever need. In practice, stop at two levels and use structs beyond that.
A pointer to a pointer is a variable that holds the address of another pointer, declared with ** and read by dereferencing twice. Use it when a function has to change the caller's pointer, as in allocation helpers, and when you work with arrays of pointers such as argv and string tables. Check for NULL at each level, free memory in reverse order of allocation, and keep indirection to two levels where possible.