How to swap two numbers without using a third variable

How to swap two numbers without using a third variable

Using pointers you can easily swap two variables without using an additional variable to store a temporary value. This is because instead of swapping values, you can swap addresses, as shown in this function:

void SwapSmart(int * a, int * b)
{
*a = *a xor *b;
*b = *b xor *a;
*a = *a xor *b;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top