Pointer to pointer in C: solved exercise
If you searched for pointer to pointer in C solved exercise, this page shows how to update an original pointer inside a function.
Problem statement
Create a function that receives int **p and redirects *p to another integer.
C solution
#include <stdio.h>
void redirect(int **p, int *new_target) {
*p = new_target;
}
int main(void) {
int a = 10;
int b = 99;
int *p = &a;
printf("Before: %d\n", *p);
redirect(&p, &b);
printf("After: %d\n", *p);
return 0;
}Expected output
Before: 10
After: 99Practical use
- functions that allocate memory and return pointers,
- linked-list head insert/delete operations,
- APIs that need to rewrite references.
Common mistakes
- Passing
pinstead of&p. - Mixing up
p,*p, and**p. - Dereferencing uninitialized pointers.
Recommended next exercise
- Malloc and free in C: solved dynamic memory exercise
- Pointers in C: solved pass-by-reference exercises
- Binary tree in C: solved insertion and search exercise
- All C exercises
Guided practice and full book
If you want a complete path with progressive difficulty:
FAQ
Is this exercise useful for C exams and technical interviews?
Yes. It targets patterns that commonly appear in practice assignments, technical interviews, and C programming exams.
Where can I keep practicing with more solved C exercises?
In Programming in C in 100 Solved Exercises and C Exercises. Kindle Unlimited: View on Amazon.
How should I practice this exercise type to improve faster?
Start with small inputs, run edge cases (empty, one item, max capacity), then rewrite the solution from scratch without copying.