Pointers to pointers let you store the address of a pointer. This helps when you want to change a pointer inside a function or manage multiple levels of data.
When you want a function to change the address stored in a pointer.
When working with arrays of strings (like list of words).
When managing dynamic multi-dimensional arrays.
When you need to pass a pointer by reference to another function.
Syntax
C
type **pointer_to_pointer;
Each * adds one level of indirection (address pointing).
Use ** to declare a pointer that points to another pointer.
Examples
Declares a pointer to a pointer to an int.
C
int **pptr;
Declares a pointer to a pointer to a char (often used for strings).
C
char **str_ptr;
Pointer pptr stores the address of pointer ptr.
C
int *ptr;
int **pptr = &ptr;
Sample Program
This program shows how a pointer to a pointer can access and change the original value.
C
#include <stdio.h>
int main() {
int value = 10;
int *ptr = &value; // pointer to intint **pptr = &ptr; // pointer to pointer to int
printf("Value: %d\n", value);
printf("Value via ptr: %d\n", *ptr);
printf("Value via pptr: %d\n", **pptr);
// Change value using pointer to pointer
**pptr = 20;
printf("New value after change via pptr: %d\n", value);
return0;
}
OutputSuccess
Important Notes
Remember to use ** to access the value when using a pointer to a pointer.
Pointer to pointer is useful for modifying pointers inside functions.
Summary
Pointer to pointer stores the address of another pointer.
Use ** to declare and access pointer to pointer values.
It helps when you want to change pointers or work with multi-level data.
Practice
(1/5)
1. What does a pointer to a pointer store in C?
easy
A. The size of a variable
B. The value of an integer
C. The address of another pointer
D. The address of a function
Solution
Step 1: Understand pointer basics
A pointer stores the address of a variable.
Step 2: Understand pointer to pointer
A pointer to pointer stores the address of another pointer, not a direct value.
Final Answer:
The address of another pointer -> Option C
Quick Check:
Pointer to pointer = address of pointer [OK]
Hint: Pointer to pointer holds address of pointer, not value [OK]
Common Mistakes:
Confusing pointer to pointer with pointer to value
Thinking it stores the value directly
Mixing it with function pointers
2. Which of the following is the correct way to declare a pointer to a pointer to an int in C?
easy
A. int **ptr;
B. int ptr*;
C. int *ptr*;
D. int &ptr*;
Solution
Step 1: Recall pointer declaration syntax
Single pointer to int is declared as int *ptr;.
Step 2: Extend to pointer to pointer
Pointer to pointer uses two asterisks: int **ptr;.
Final Answer:
int **ptr; -> Option A
Quick Check:
Pointer to pointer declaration uses two * [OK]
Hint: Use two * for pointer to pointer declaration [OK]
Common Mistakes:
Placing * incorrectly in declaration
Using & instead of *
Confusing pointer syntax with references
3. What is the output of the following C code?
int x = 10;
int *p = &x;
int **pp = &p;
printf("%d", **pp);
medium
A. Address of x
B. Compilation error
C. Address of p
D. 10
Solution
Step 1: Understand variable assignments
x = 10, p points to x, pp points to p.
Step 2: Evaluate **pp
*pp gives p (pointer to x), **pp gives value at x, which is 10.
Final Answer:
10 -> Option D
Quick Check:
**pp dereferences twice to get x's value [OK]
Hint: Double dereference **pp gets original value [OK]
Common Mistakes:
Printing pointer address instead of value
Using single * instead of **
Confusing *p and **pp
4. Identify the error in the following code snippet:
int x = 5;
int *p = &x;
int **pp;
*pp = p;
printf("%d", **pp);
medium
A. Syntax error in printf
B. pp is not initialized before dereferencing
C. x is not initialized
D. p is not assigned correctly
Solution
Step 1: Check pointer initialization
pp is declared but not initialized to point anywhere.
Step 2: Analyze dereferencing *pp = p;
Dereferencing uninitialized pp causes undefined behavior or crash.
Final Answer:
pp is not initialized before dereferencing -> Option B