C Pointers to Pointers
Use a pointer-to-pointer when an operation must select a different object for its caller.
How it works
Passing an int * gives a helper access to one int, but assigning the helper parameter cannot replace the caller pointer object. Pass the address of that pointer when reassignment is part of the contract. The helper then dereferences once to reach the caller pointer, and twice to reach an int if appropriate. Keep the levels explicit and avoid a generic void ** cast: pointer-to-pointer conversions do not automatically provide type-safe generic storage.
Run it locally
Use a hosted C17 compiler in a separate lab folder. The command assumes a driver named cc; substitute gcc, clang or zig cc as appropriate. C does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed for these C programs.
cc -std=c17 -Wall -Wextra -Wpedantic -Werror pointer-to-pointer.c -o lessonOn Windows, use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Math-library linking differs by toolchain; GCC on many Unix systems needs -lm after the source.
Download the complete C source. File-writing labs must run in an empty private directory, away from existing reports.
Example
#include <stdio.h>
#include <stdbool.h>
static bool choose_backup(int **out, int *backup) {
if (out == NULL || backup == NULL) { return false; }
*out = backup;
return true;
}
int main(void) {
int first = 11;
int backup = 16;
int *chosen = &first;
if (!choose_backup(&chosen, &backup)) { return 1; }
printf("Chosen: %d\n", *chosen);
return 0;
}
Expected result
Chosen: 16
&chosen points to the actual pointer variable in main. The helper stores backup into that variable; the caller then dereferences the new selection. Both candidate ints remain alive for this entire operation. This example transfers a reference to existing storage, not ownership of allocated memory, so no free call is required.
For language and library rules, consult the WG14 C working group and its public C11 committee draft, which describes the core facilities used here; C17 incorporates corrections. Check your compiler and runtime documentation for implementation details.