swap(x,y)

FLEA jsb at advdev.LBP.HARRIS.COM
Wed Aug 23 05:12:30 AEST 1989


In article <8350 at boring.cwi.nl> tromp at piring.cwi.nl (John Tromp) writes:
:		 (Timothy R. Gottschalk) writes:
:
:>       To swap two variables x,y in C without using a temporary variable:
:
:>       /* begin swap */
:>       x += y;
:>       y = x - y;
:>       x -= y;
:>       /* end swap */
:
:>       Just curious...(this looks pretty valid) does anyone know an even
:
:How about
:
:x^=y^=x^=y;
:
:Avoids possible overflow problems and looks great in an
:Obfuscated C Code entry too!

Sorry, folks, both of these are illegal if x and y are pointers.



More information about the Comp.lang.c mailing list