macros and semicolons

der Mouse mouse at thunder.mcrcim.mcgill.edu
Sat Jun 29 22:53:14 AEST 1991


In article <6531 at goanna.cs.rmit.oz.au>, ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
> In article <160662 at pyramid.pyramid.com>, markhall at pyrps5.pyramid.com (Mark Hall) writes:
>> NO macro will work for a swap.

>  Er, this turns out not to be the case.  Consider
> 	#define swap(Type, This, That)				\
> 	    do {						\
> 		Type *ThisQZ = &(This), *ThatQZ = &(That), t;	\ 
> 		t = *ThisQZ, *ThisQZ = *ThatQZ, *ThatQZ = t;	\
> 	    } while (0)

That won't work because of the space following the third backslash.

More seriously, that won't work if an argument doesn't have an address
(eg, is a bitfield or a register variable) or one of the last two
arguments happens to be called ThisQZ or ThatQZ.

It also breaks if ThisQZ, ThatQZ, or t happens to be #defined to
something other than a simple identifier.

I'll stand with markhall on this one, at least until C gets gensyms and
typeof.  (Yes, I know gcc already has typeof.  C doesn't.)

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list