Is this swap() macro correct?

Eric Rouse eric at tqc.FIDONET.ORG
Fri Jan 19 01:33:01 AEST 1990


 > Message-ID: <21068 at stealth.acf.nyu.edu>
 >
 > Say swap() is defined as
 >
 > #define swap(x,y,typ) block typ *_MV_x = x; typ *_MV_y = y; typ tmp;\
 >                     tmp = *_MV_x; *_MV_x = *_MV_y; *_MV_y = tmp;

      As long as your working with base types, or types you can cast to a base 
types like integers, longs etc, you might look into this one:

#define Swap(X1,X2) { X1 ^= X2; X2 ^= X1; X1 ^= X2; }

      It's quite nice for integers, unsigned, longs, pointers etc.  Plus it 
doesn't use any temp storage.

Eric Rouse -- via The Q Continuum (FidoNet Node 1:382/31)
UUCP: ...!rpp386!tqc!eric
ARPA: eric at tqc.FIDONET.ORG



More information about the Comp.lang.c mailing list