Pointer to pointer in C: solved exercise with reference update

  2 minutes

If you searched for pointer to pointer in C solved exercise, this page shows how to update an original pointer inside a function.

Create a function that receives int **p and redirects *p to another integer.

#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;
}
Before: 10
After: 99
  • functions that allocate memory and return pointers,
  • linked-list head insert/delete operations,
  • APIs that need to rewrite references.
  • Passing p instead of &p.
  • Mixing up p, *p, and **p.
  • Dereferencing uninitialized pointers.

If you want a complete path with progressive difficulty:

Yes. It targets patterns that commonly appear in practice assignments, technical interviews, and C programming exams.

In Programming in C in 100 Solved Exercises and C Exercises. Kindle Unlimited: View on Amazon.

Start with small inputs, run edge cases (empty, one item, max capacity), then rewrite the solution from scratch without copying.