Variables, references and pointers - Write the function set (…) so that the following code works correctly
You have too many variables. The ret is not needed.

Also, for testing I would like to see some actual testing, rather than just printing to the console and letting the user do it:

#include &lt;cassert&gt;

void test_set() {
    int x = 3;
    set(x) = 3;
    assert(x == 0);
    set(x) = 4;
    assert(x == 3);
    // etc.
}


(Or, you could use an actual unit testing framework. But assert works for this simple case.)