Is this swap() macro correct?

Mike Schilling mikes at rtech.UUCP
Fri Jan 19 04:15:04 AEST 1990


>From article <1990Jan18.002842.441 at aqdata.uucp>, by sullivan at aqdata.uucp (Michael T. Sullivan):
> From article <21068 at stealth.acf.nyu.edu>, by brnstnd at stealth.acf.nyu.edu:
>> Say swap() is defined as
>> 
>> #define block do {
>> #define endblock } while(0)
>> #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; endblock
> 
> Why are "block" and "endblock" even bothered with here.  Why not just
> put the "do {" and "} while (0)" in the definition of swap?  Could
> somebody please enlighten me.
> -- 
In fact why use the do-while at all, and why use pointers?  The classic swap
macro is

# define swap(x, y, typ) 	\
	{			\
		typ tmp; 	\
				\
		tmp = y; y = x; x = tmp;	\
	}

Is this just as good, or am I being *exceptionally* dense today?



More information about the Comp.lang.c mailing list