Pointer to pointer in C: solved exercise with reference update

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#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

1
2
Before: 10
After: 99

Practical use

  • functions that allocate memory and return pointers,
  • linked-list head insert/delete operations,
  • APIs that need to rewrite references.

Common mistakes

  • Passing p instead of &p.
  • Mixing up p, *p, and **p.
  • Dereferencing uninitialized pointers.

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.