Why use double reference (**x)?

Observe that this usually occurs for malloc.

main

int *x = malloc(...);

Suppose we have a function:

void f(int *x) {
    x = malloc(...);
}

In the above case, “x” is bound to local scope, as such in main the x is unchanged.

(main)
x --- | mem |
    /
   /
  /
 /
x (scoped locally)

When we do x = malloc(…)

(main)
x ---| mem  |
     | ...  |
     | mem2 |
    /
   /
  /
 /
x (scoped locally)

We can see that the above x is unchanged.

If we want to reassign above x, we can do:

x (scoped locally) --- x (main) --- | mem |