๐Ÿ“ C Pointers
Estimated reading: 4 minutes 7 views

๐Ÿ” 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 value
  • p holds the address of a
  • pp holds the address of p

๐Ÿ’ป 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 = &num;
    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?

ScenarioBenefit
Function modifies a pointerYou need to pass &pointer (i.e., **)
Dynamic 2D arraysHandle array of pointers (rows)
argv in main()Access string arguments dynamically
Pointers to structs or arraysManage 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ” C Pointer to Pointer

Or Copy Link

CONTENTS
Scroll to Top