Recently I came across this question write a function to swap two numbers without using extra space? The function can be in two ways:
int swap ( int *a, int* b)
{
*a = *a+*b;
*b = *a-*b;
*a = *a-*b;
}
The other way is XOR operation:
int swap ( int *a, int* b)
{
*a = *a^*b;
*b = *a^*b;
*a = *a^*b;
}
Even though both these functions are a good idea, they will not work if both of a and b point to a single memory location? How to tackle this?