macros and semicolons

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Fri Jun 28 09:51:21 AEST 1991


In article <6531 at goanna.cs.rmit.oz.au> ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
> 	#define swap(Type, This, That)				\
> 	    do {						\
> 		Type *ThisQZ = &(This), *ThatQZ = &(That), t;	\ 
> 		t = *ThisQZ, *ThisQZ = *ThatQZ, *ThatQZ = t;	\
> 	    } while (0)

You should call this SWAP. Without the uppercase, someone might be
fooled into thinking that it works like a function. Namespace issues
aside, there are three problems with this as a function: (1) You're
using This and That by reference. (2) You're passing a Type argument.
(3) This is a statement rather than an expression.

SWAP(x,y,int) would be fine, though I don't think you lose anything by
making the user indirect x and y: SWAP(&x,&y,int).

---Dan



More information about the Comp.lang.c mailing list