To be clear, you're not "passing by reference" in the C++ definition here. You're passing a pointer to your values to scanf
. C++ pass-by-reference is declared on the callee side, and happens implicitly on the caller side; the caller doesn't need to, and usually can't, use the &
(address-of) operator at all (not without undoing it in the same expression).
In any event, if you pass the raw values to scanf
, it receives a copy of the value, not a reference or pointer to it (scanf
is a C function that was inherited by C++; it can't receive C++ references at all), and has no idea where it came from, so it can't find your a
and b
to write to.
Worse, it tries to interpret the value it received as a pointer to where it should write the parsed value, which is highly likely to cause a segfault as it tries to write to unallocated memory (if you're unlucky, it writes to some random valid memory address and now you've got subtle memory corruption issues lurking in your code instead of obvious crashes).
Note: If by "done it with actual variables" you mean you passed pa
and pb
instead of &a
and &b
, that would work just fine, since pa
stores the same pointer &a
produces, and scanf
just needs the pointer, it doesn't care where it comes from; they're interchangeable here (they may or may not be for update
, depending on whether update
receives pointers, or pointer references).