I found the following declaration for a generic swap function in an answer on stackoverflow.
#define swap(x,y) do \
{ unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \
memcpy(swap_temp,&y,sizeof(x)); \
memcpy(&y,&x, sizeof(x)); \
memcpy(&x,swap_temp,sizeof(x)); \
} while(0)
However if we write a similar function in C it does not work and we need to pass in the size of the void* types explicitly for it to work. I understand that this would be happening because the macro would be expanded within the function from which it is called and sizeof would work since it would be in the same scope as the variable x or y. Am I correct?
If I am. Consider the following situation.
void someComplexFunction(void* a, void* b){
swap(a,b);
}
Is this right? I tried it and it worked. Why is this working?