Path: utzoo!utgpu!water!watmath!clyde!rutgers!lll-lcc!pyramid!decwrl!cookie.dec.com!devine From: devine@cookie.dec.com (Bob Devine) Newsgroups: comp.lang.c Subject: swap macro -- beware Message-ID: <8801200422.AA19420@decwrl.dec.com> Date: 20 Jan 88 04:22:50 GMT Organization: Digital Equipment Corporation Lines: 16 > #define swap(a,b) ((a) = ((b) = ((a) = (a) ^ (b)) ^ (b)) ^ (a)) > He [the genius] says it will work for anything (int, char *, etc.). Well, yes and no. This old trick will work for those types that are defined for the ^ op. That means structs, arrays, unions, bitfields, and floats won't work the way you want. Something else that will bite you if you use this macro is sign-extension, examine what happens when swapping an int with a signed char whose top bit is on. It may not result in what you thought it would. The preprocessor does not know C and does not follow type rules. The endless discussion of a generic swap macro some time ago usually ended with the person bemoaning the lack of a "typeof()" operator to handle the non-word-sized types. exit(Bob) -- the unique and naturally refreshing choice for an exit value