๐ C Pointer to Pointer โ Mastering Double Indirection in C
๐งฒ Introduction โ What Is a Pointer to Pointer in C?
In C programming, a pointer to pointer (also known as double pointer) is a variable that stores the address of another pointer. This concept allows multiple levels of indirection and is crucial in advanced programming such as dynamic 2D arrays, modifying function pointers, and handling command-line arguments.
๐ฏ In this guide, youโll learn:
- The syntax and use of
int **ptr
and similar constructs - When and why to use a pointer to pointer
- How to access and modify values at multiple levels
- Common use cases, examples, and pitfalls
๐ Core Concept โ What Is Double Indirection?
A regular pointer stores the address of a variable. A pointer to pointer stores the address of a pointer:
int a = 10;
int *p = &a; // pointer to int
int **pp = &p; // pointer to pointer to int
a
holds a valuep
holds the address ofa
pp
holds the address ofp
๐ป Code Examples โ Pointer to Pointer in Practice
โ Example 1: Basic Usage of Pointer to Pointer
#include <stdio.h>
int main() {
int val = 50;
int *p = &val;
int **pp = &p;
printf("Value: %d\n", val); // 50
printf("*p: %d\n", *p); // 50
printf("**pp: %d\n", **pp); // 50
return 0;
}
โ Example 2: Modifying Value via Double Pointer
#include <stdio.h>
void modify(int **q) {
**q = 100;
}
int main() {
int num = 20;
int *ptr = #
int **pptr = &ptr;
modify(pptr);
printf("Modified Value: %d\n", num); // Output: 100
return 0;
}
๐ Double pointers allow functions to modify values indirectly, useful in callback or configuration APIs.
โ Example 3: Application in Command-Line Arguments
#include <stdio.h>
int main(int argc, char **argv) {
printf("Program name: %s\n", argv[0]);
if (argc > 1) {
printf("First argument: %s\n", argv[1]);
}
return 0;
}
๐ Here, argv
is a char **
โ a pointer to an array of character pointers (strings).
๐ง Why Use Pointer to Pointer?
Scenario | Benefit |
---|---|
Function modifies a pointer | You need to pass &pointer (i.e., ** ) |
Dynamic 2D arrays | Handle array of pointers (rows) |
argv in main() | Access string arguments dynamically |
Pointers to structs or arrays | Manage memory efficiently |
๐ก Best Practices & Tips
๐ Best Practice:
Use meaningful names like pp
, ptr_to_ptr
, argv
for clarity.
๐ก Tip:
If you need to change the address a pointer holds inside a function, pass a pointer to that pointer.
โ ๏ธ Pitfall:
Dereferencing a pointer to pointer (**ptr
) without initializing it leads to segmentation faults or undefined behavior.
๐ ๏ธ Real-World Applications
- ๐งฎ Dynamic allocation of 2D arrays (
int **matrix
) - ๐งช Functions that modify passed-in pointers (e.g.,
char **buffer
) - ๐ฅ๏ธ Command-line interfaces (via
char **argv
) - ๐ฆ Advanced memory management systems
- ๐งฑ Managing linked structures like nodes in trees/graphs
๐ Summary โ Recap & Next Steps
Pointers to pointers provide another level of memory control and abstraction. They are essential in many system-level and dynamic programming scenarios.
๐ Key Takeaways:
int **pp
is a pointer to pointer to int- Used for modifying pointers, 2D arrays, and CLI arguments
- Access value:
**pp
, address of pointer:*pp
, address of data:pp
- Must be properly initialized before dereferencing
โ๏ธ Real-World Relevance:
Used in network buffers, argument parsers, memory management layers, and multi-dimensional dynamic data handling.
โ Frequently Asked Questions (FAQ)
โ What is a pointer to pointer in C?
โ Itโs a pointer that stores the address of another pointer, allowing double indirection.
โ Why do we use pointer to pointer?
โ To modify the value of a pointer from within a function or to handle complex data structures like 2D arrays.
โ How do I declare and use a pointer to pointer?
โ Syntax:
int **pp;
*pp = &var1;
**pp = 10;
โ Can we have triple pointers in C?
โ
Yes, C supports multiple levels of indirection: int ***ppp
, etc. Though rarely used, they can manage deeper levels of data abstraction.
โ Is char **argv
a pointer to a pointer?
โ Yes. Itโs a pointer to an array of character pointersโeach pointing to a string argument.
Share Now :