In C programming, a double pointer is a pointer that points to another pointer. It is also referred to as a pointer-to-pointer. 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.

Table of Contents

How to declare a pointer-to-pointer?

The syntax to declare a double pointer in C is:

In this example, the variable doubleptr is a double pointer to an integer value. The ** notation is used to indicate that doubleptr is a pointer to a pointer. To initialize a double pointer, you can use the address of operator & on a pointer variable, like this:

How to initialize a double pointer?

C Program to show how double pointer works?

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

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

Use Double Pointer to create Two Dimensional Array

Here is an example that demonstrates how to use double pointers to create a dynamic two-dimensional array. This program demonstrates how double pointers can be used to create complex data structures with dynamic memory allocation in C programming.

This program prompts the user to enter the number of rows and columns for a matrix (two dimensional array), then dynamically allocates memory for the matrix using double pointers. It fills the matrix with values, prints it to the console, and then frees the memory.

Uses of Pointer-to-Pointer (Double Pointer)

Double pointers serve a variety of purposes in C Programming. Here are some of the most common uses of double pointers and why you might want to use them:

  • Dynamic Memory Allocation: Double pointers are often used to allocate memory for arrays, particularly multi-dimensional arrays. By using a double pointer, you can ensure that the memory allocation is protected even outside of a function call. To allocate space for a matrix or multi-dimensional arrays dynamically, you will need a double-pointer.
  • Passing Handles Between Functions: This is particularly useful if you need to pass re-locatable memory between functions.
  • Characters List: 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.
  • Main Function Parameters: 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.
  • Linked Lists: Double pointers are frequently used in linked list operations, such as insertion and deletion.

If you’re interested in learning more about double pointers, there are many helpful resources available online. You might start by reading some of the articles on StackOverflow or Quora.

Common Errors with Double Pointers in C

Here are some common errors with double pointers that new programmers make while write C Programming code:

  1. Incorrect Syntax: One common mistake when working with double pointers is using the wrong syntax. This can result in compilation errors or unexpected behavior in the program. For example, forgetting to include the ‘*’ operator when dereferencing a double pointer can cause the program to crash.
  2. Dereferencing Errors: Another common error is dereferencing incorrectly. This can happen when trying to access memory that hasn’t been allocated or has already been freed. Forgetting to check for NULL values can also lead to segmentation faults.
  3. Memory Allocation Errors: Double pointers are often used for dynamic memory allocation in C programming, but errors can occur when allocating memory. Common mistakes include not allocating enough memory, allocating too much memory, or failing to free memory when it’s no longer needed.
  4. Type Mismatch Errors: Double pointers can also cause type mismatch errors if they’re not properly cast to the correct type. This can happen when passing double pointers to functions or when assigning values to double pointers.
  5. Pointer Arithmetic Errors: Pointer arithmetic can be tricky in C programming, especially when working with double pointers. Common errors include using incorrect operators or not correctly accounting for the size of the data type.

By avoiding these common errors with double pointers in C programming, you can write better C programs. This will also help to improve your overall understanding of double pointers and enhance your programming skills further.