The problem is obvious in mathematics.
int swap ( int *a, int* b) { *a = *a+*b; *b = *a-*b; *a = *a-*b; }
when a and b point to the same place, then the code above turns into
*a = *a + *a;
So both *a and *b are zero. To do this, you must access using if :
int swap ( int *a, int* b) { if (a!=b) { *a = *a+*b; *b = *a-*b; *a = *a-*b; } }
And the same for xor version.
deepmax
source share