macros and semicolons

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Thu Jun 27 15:45:09 AEST 1991


In article <160662 at pyramid.pyramid.com>, markhall at pyrps5.pyramid.com (Mark Hall) writes:
> Forgive me for this unsufferable horn-tooting; I just couldn't resist
> (I also couldn't believe no one else sent this reply :-). . . .
> NO macro will work for a swap.  You suffer from the call-by-name rule
> which undid ALGOL in this case.  Consider the expansion of SWAP(i,a[i]):
> 
> 	int c; c = i; i = a[i]; a[i] = c;

 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)

What happens to swap(int, i, a[i])?
	do { int *ThisQZ = &i, *ThatQZ = &a[i], t;
	     t = *ThisQZ, *ThisQZ = *ThatQZ, *ThatQZ = t;
	} while (0);

This is essentially what you'd get from inlining a call to a swap
procedure with reference parameters.
-- 
I agree with Jim Giles about many of the deficiencies of present UNIX.



More information about the Comp.lang.c mailing list