A double pointer adds another level of indirection and makes it possible to work with memory more efficiently in advanced programming scenarios. Double pointers are widely used in dynamic memory allocation, multidimensional arrays, linked data structures, and functions that need to modify pointer variables.
Understanding double pointers is an important step toward mastering memory management and advanced programming techniques in C.
Table of Contents
What is a Double Pointer in C?
A double pointer is a pointer that stores the address of another pointer. In other words, it points to a pointer variable instead of directly pointing to a normal variable.Consider a situation where an integer variable stores a value. A normal pointer can store the address of that integer variable. A double pointer can then store the address of the pointer. This creates two levels of referencing between the double pointer and the actual data.
- The first level accesses the pointer, while the second level accesses the original variable.
- Because of this additional level of indirection, a double pointer is often referred to as a pointer to a pointer.
Syntax:
Example:data_type **pointer_name;
// C program to implement double pointer
#include stdio.h
int main()
{
int num = 10;
int *ptr = #
int **dptr = &ptr;
printf("Value of num = %d\n", num);
printf("Value using ptr = %d\n", *ptr);
printf("Value using dptr = %d\n", **dptr);
return 0;
}
Output:
Explanation:Value of num = 10
Value using ptr = 10
Value using dptr = 10
In this program, the variable num stores the value 10. The pointer ptr stores the address of num, while the double pointer dptr stores the address of ptr.
When the expression **dptr is evaluated, the first dereference accesses ptr, and the second dereference accesses the value stored in num. Therefore, the value 10 is displayed.
Representation of a Double Pointer
Every variable occupies a memory location and has a unique address. A pointer stores the address of a variable. A double pointer stores the address of that pointer. Therefore, the double pointer indirectly reaches the original variable through the pointer.Consider the following declarations:
Assume the following memory layout:int num = 10;
int *ptr = #
int **dptr = &ptr;
| Address | Variable | Value |
|---|---|---|
| 1000 | num | 10 |
| 2000 | ptr | 1000 |
| 3000 | dptr | 2000 |
In this representation:
- The variable num is stored at address 1000 and contains the value 10.
- The pointer ptr is stored at address 2000 and contains the address of num.
- The double pointer dptr is stored at address 3000 and contains the address of ptr.
Accessing values:dptr → ptr → num → 10
The double pointer first accesses the pointer and then follows that pointer to reach the original variable. This two-step process is known as double indirection.num = 10
*ptr = 10
**dptr = 10
Declaring and Initializing a Double Pointer
A double pointer is declared using two asterisks (**). The first asterisk indicates that the variable is a pointer, and the second asterisk indicates that it points to another pointer.Merely declaring a double pointer does not make it usable. Like any other pointer, it must be initialized with a valid address before being accessed. A double pointer should store the address of a pointer variable rather than the address of a normal variable.
Syntax:
Example:data_type **pointer_name;
Below is a C program to declare and initialize a double pointer and display the value stored in a variable using the double pointer:
// C program to display the value of
// a variable using double pointer
#include stdio.h
int main()
{
int x = 50;
int *ptr = &x;
int **dptr = &ptr;
printf("%d\n", **dptr);
return 0;
}
Output:
Explanation:50
The variable x stores the value 50. The pointer ptr stores the address of x. The double pointer dptr stores the address of ptr.
When **dptr is used, the first dereference accesses ptr, and the second dereference accesses the value of x. As a result, the program displays 50.
Accessing Values Using a Double Pointer
A double pointer requires two dereference operators to access the actual data stored in a variable. The first dereference retrieves the pointer, and the second dereference retrieves the value pointed to by that pointer.Example:
Below is the C program to access a variable using a double pointer:
// C program to access a variable
// using a double pointer
#include stdio.h
int main()
{
int num = 25;
int *ptr = #
int **dptr = &ptr;
printf("Using variable: %d\n", num);
printf("Using pointer: %d\n", *ptr);
printf("Using double pointer: %d\n", **dptr);
return 0;
}
Output:
Explanation:Using variable: 25
Using pointer: 25
Using double pointer: 25
The same value can be accessed directly through the variable, indirectly through a pointer, or through a double pointer. Although the methods are different, all three statements retrieve the same data.
Modifying a Variable Using a Double Pointer
A double pointer can be used not only to access data but also to modify it. Any change made through a double pointer affects the original variable because the double pointer ultimately refers to that variable.Example:
Below is the C program to modify the value of a variable using a double pointer:
// C program to modify the value of
// the variable using a double pointer
#include stdio.h
int main()
{
int num = 30;
int *ptr = #
int **dptr = &ptr;
**dptr = 100;
printf("Value of num = %d\n", num);
return 0;
}
Output:
Explanation:Value of num = 100
The statement **dptr = 100 reaches the original variable through two levels of indirection and updates its value. Therefore, the value of num becomes 100.
Double Pointer with Functions
One of the most important applications of double pointers is modifying pointer variables inside functions. Since C uses call by value, changes made to a pointer parameter do not affect the original pointer unless its address is passed.Example:
Below is a C program that changes the address stored in a pointer using a function:
// C program to change the address
// stored in a pointer using a function
#include stdio.h
void updatePointer(int **p, int *newAddress)
{
*p = newAddress;
}
int main()
{
int a = 10;
int b = 20;
int *ptr = &a;
printf("Before: %d\n", *ptr);
updatePointer(&ptr, &b);
printf("After: %d\n", *ptr);
return 0;
}
Output:
Explanation:Before: 10
After: 20
Initially, the pointer points to variable a. The address of the pointer itself is passed to the function. Inside the function, the pointer is updated to store the address of b. As a result, after returning from the function, the pointer accesses the value stored in b.
Double Pointer and Dynamic Memory Allocation
Double pointers are frequently used in dynamic memory management, especially when allocating memory for arrays, matrices, and other complex structures.Example:
Below is a C program to allocate memory dynamically and access the allocated value using a double pointer:
// C program to allocate memory dynamically
#include stdio.h
#include stdlib.h
int main()
{
int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 500;
int **dptr = &ptr;
printf("Value = %d\n", **dptr);
free(ptr);
return 0;
}
Output:
Explanation:Value = 500
Memory is allocated dynamically using malloc(). The pointer stores the address of the allocated memory block, while the double pointer stores the address of the pointer. Using **dptr, the program accesses the value stored in dynamically allocated memory.
Applications of Double Pointers
Double pointers have several practical applications in C programming.- Dynamic Memory Allocation: Double pointers are used when allocating memory for multidimensional arrays and dynamically created data structures.
- Modifying Pointers in Functions: They allow functions to change the address stored in a pointer variable.
- Dynamic Two-Dimensional Arrays: Double pointers help create and manage dynamic matrices.
- Linked Lists and Trees: Many operations on linked lists and trees require modifying node pointers, making double pointers extremely useful.
- Command-Line Arguments: The argv parameter in the main() function is implemented using a pointer to a pointer.
int main(int argc, char **argv)
Common Mistakes While Using Double Pointers
1. Accessing Data with a Single Dereference: Many beginners mistakenly use a single dereference operator when accessing data through a double pointer.The above statement returns a pointer value rather than the actual integer value.printf("%d", *dptr);
Correct approach:
2. Using an Uninitialized Double Pointer: A double pointer should always be initialized before use.printf("%d", **dptr);
Since dptr contains a garbage address, the program may crash or produce unpredictable results.int **dptr;
printf("%d", **dptr);
3. Assigning the Address of a Variable Instead of a Pointer: A double pointer must store the address of a pointer variable.
4. Forgetting to Release Dynamically Allocated Memory: Memory allocated using malloc() should always be released using free() when it is no longer needed.Incorrect:
int num = 10;
int **dptr = #
Correct:
int *ptr = #
int **dptr = &ptr;
Failing to release memory can result in memory leaks.free(ptr);
Pointer vs Double Pointer
| Basis of Comparison | Pointer | Double Pointer |
|---|---|---|
| Definition | A pointer stores the address of a variable. | A double pointer stores the address of another pointer. |
| Declaration Syntax | It requires a single asterisk (*) during declaration. | It requires two asterisks (**) during declaration. |
| Dereferencing Required | One dereference operator is used to access data. | Two dereference operators are used to access data. |
| Memory Relationship | It points directly to a variable. | It points to a pointer that points to a variable. |
| Common Usage | It is commonly used for basic memory access. | It is commonly used for advanced memory management. |
| Example Declaration | int *ptr; | int **dptr; |
| Example Access | *ptr accesses the stored value. | **dptr access the stored value. |
Conclusion
A double pointer in C is a pointer that stores the address of another pointer. It introduces an additional level of indirection that allows programmers to work with pointers more effectively. Double pointers are widely used in dynamic memory allocation, multidimensional arrays, linked data structures, and functions that need to modify pointers. A clear understanding of double pointers helps build a strong foundation for advanced C programming and memory management concepts.Frequently Asked Questions
1. What is a double pointer in C?2. Why are double pointers used in C?A double pointer is a pointer that stores the address of another pointer.
3. How do you declare a double pointer?Double pointers are used for dynamic memory allocation, modifying pointers inside functions, and implementing advanced data structures.
4. What does **dptr represent?int **dptr;
5. What is the difference between a pointer and a double pointer?It performs two levels of dereferencing and accesses the original value stored in a variable.
A pointer stores the address of a variable, whereas a double pointer stores the address of another pointer.
0 Comments