A pointer in C is a variable that represents the location of an item, such as a variable or an array. We use pointers to pass information back and forth between a function and its reference point.

Sometimes, a pointer can be declared to point to another pointer which points to a variable. Here, the first pointer contains the address of the second pointer. The second pointer points to an actual memory location where the data is stored, i.e. a variable. That’s the reason why we also call such pointers as double pointers.

The syntax to declare a double pointer in C is:

int **doubleptr;

The following C program shows how pointer-to-pointer works by printing the pointer address as well as variable address and values:

#include 

int main() 
{ 
    int var = 120; 
    int *varptr = &var; 
    int **doubleptr = &varptr; 
      
    printf("Value of var = %d\n", var ); 
    printf("Value of var pointer = %d\n", *varptr ); 
    printf("Value of double pointer = %d\n", **doubleptr); 
    
    printf("Address of var = %p\n", &var ); 
    
    printf("Address of var pointer = %p\n", &varptr ); 
    printf("Value in var pointer = %p\n", varptr );
    
    printf("Address of double pointer = %p\n", *doubleptr);
    printf("Value in double pointer = %p\n", doubleptr);
    
  return 0; 
}  

The output of the above C Program is:

Value of var = 120
Value of var pointer = 120
Value of double pointer = 120
Address of var = 0x7ffe25192bfc
Address of var pointer = 0x7ffe25192c00
Value in var pointer = 0x7ffe25192bfc
Address of double pointer = 0x7ffe25192bfc
Value in double pointer = 0x7ffe25192c00

Uses of Pointer-to-Pointer (Double Pointer)

  • We use double-pointer when we want to protect the memory allocation or an assignment even outside of a function call.
  • To allocate space for a matrix or multi-dimensional arrays dynamically, you will need a double-pointer.
  • Another use is that if you want to have a list of characters (a word), you can use char *word, for a list of words (a sentence), you can use char **sentence, and for list of sentences (a paragraph), you can use char ***paragraph.
  • We can use Pointers to pointers as “handles” to memory where you want to pass around a “handle” between functions to re-locatable memory.
  • One of the most common uses (every C programmer should have encountered) as parameters to the main() function, i.e. int main(int argc, char **argv). Here argv is the array of arguments passed on to this program.
  • The linked list uses a double-pointer in different operations such as insertion and deletion.
  • Here are some good reads on StackOverFlow and Quora about the uses of double pointers.