Introduction

Pointers to pointers, also known as double pointers, allow programmers to work with multiple levels of indirection in memory. They are commonly used in dynamic memory management, multi-dimensional arrays, and handling function arguments in C.

Why Study This Topic?

Understanding pointer-to-pointer is crucial in C programming because they unlock advanced operations like dynamic memory allocation, linked data structures, and modifying variables outside function scope.
Real-time example: Imagine you’re the manager of a hotel. You have a receptionist (pointer) who has access to the list of room keys (addresses). Now, you (pointer-to-pointer) can instruct the receptionist where to look—this extra layer of control is what double pointers offer in programming.

What Will Be Covered?

  • What is a pointer-to-pointer
  • Syntax and declaration
  • How memory is managed
  • Code examples and diagrams
  • Interview questions
  • Practice quizzes and exercises

What is a Pointer-to-Pointer in C?

In C programming, a pointer-to-pointer (also called a double pointer) is a type of pointer that holds the address of another pointer variable, not the address of a direct data value. This concept introduces a second level of indirection, allowing programmers to manage complex memory relationships, such as dynamically allocated arrays, multi-level data structures, and pass-by-reference scenarios for pointers.

int var = 10;
int *ptr = &var;
int **pptr = &ptr;
  • var stores the value 10
  • ptr holds the address of var
  • pptr holds the address of ptr

Syntax

datatype **pointer_name;

Example:

int **pptr;

Accessing Values with Double Pointers

printf("%d\n", **pptr); // Output: 10

You use two dereference operators (**) to reach the value stored in the original variable.

Diagram

Real-World Use Case

Double pointers are used in:

  • Dynamic allocation of 2D arrays
  • Modifying values from function parameters
  • Handling arrays of strings
  • Implementing complex data structures like linked lists, trees

Function Example Using Double Pointers

#include <stdio.h>
void update(int **pptr) {
    **pptr = 20;
}
int main() {
    int num = 10;
    int *ptr = &num;
    int **pptr = &ptr;
    update(pptr);
    printf("%d\n", num); // Output: 20
    return 0;
}

The update() function successfully changes the value of num using a pointer-to-pointer.

Summary

  • A pointer-to-pointer in c how to stores the address of another pointer
  • You use ** to access the actual data
  • Common in functions where you need to modify actual variables or allocate memory dynamically
  • Useful for 2D arrays, complex data structures, and function parameter manipulation

Learning Outcomes

By the end of this topic, learners will be able to:

  • Declare and use pointer-to-pointer variables
  • Explain how pointer-to-pointer works with memory
  • Apply double pointers in real-world coding scenarios

Common Interview Questions

Q1. What is a pointer-to-pointer in C?
A: A variable that stores the address of another pointer.
Asked in: Infosys, TCS


Q2. Why use double pointers in functions?
A: To modify pointer values or dynamically allocated memory from inside functions.
Asked in: Capgemini, Wipro


Q3. How many dereference operators are needed to access the value using a pointer-to-pointer?
A: Two (**).
Asked in: Cognizant, Tech Mahindra


Q4. Can a double pointer point to an array of pointers?
A: Yes. It is commonly used in dynamic 2D array allocation.
Asked in: HCL, Accenture


Q5. Is int **ptr2 = &ptr1; valid if ptr1 is int *?
A: Yes. ptr2 stores the address of ptr1.
Asked in: IBM, Mindtree


Practice Exercises

Coding Task

Write a program that dynamically allocates a 2D array using pointer-to-pointer, fills it with values, and prints the matrix.

Scenario

Imagine a team leader (pointer-to-pointer) managing a supervisor (pointer), who in turn assigns tasks to team members (data). The team leader can directly influence which team member works on what by instructing the supervisor. This chain of access and control is exactly how pointer-to-pointer works in C—it provides access to another pointer that manages a specific memory location.

Additional Resources

Take Quiz:

Leave a Reply

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