C/C++ Arena

A pointer to a pointer

pick_larger needs to change the caller's pointer best, not the numbers. So it takes int **out: a pointer whose arrow leads to best, which itself points at a number.

After *out = b;, best points at y. Follow the two arrows: out to best, best to y.

#include <stdio.h>

void pick_larger(int *a, int *b, int **out) {
    if (*b > *a) {
        *out = b;
    } else {
        *out = a;
    }
}

int main(void) {
    int x = 5;
    int y = 9;
    int *best = &x;
    pick_larger(&x, &y, &best);
    printf("larger = %d\n", *best);
    return 0;
}

Output:

larger = 9

From the lesson: Pointers