In attempting this HackerRank tutorial (in which you need to set a to a+b and b to |a-b|) I first attempted to modify the pointers to point to the new values:
void update(int *a,int *b) {
// Complete this function
int sum = *a + *b;
int diff = *a - *b;
if (diff < 0) diff = -diff;
a = ∑ //these do not work, because a and b are passed by value
b = &diff;
}
This doesn't work--back in main, a and b point to their original values--because (or so I understand) the pointers a and b are passed into update() by value. No problem, I thought, I'll just change the function signature to pass the pointers by reference:
void update(int *&a,int *&b) {
Sadly, this doesn't work either, but I'm at a loss for why. Can someone explain why these pointers, which appear to be passed by reference, don't get updated outside of the function's scope?