macros and semicolons

Christopher R Volpe volpe at camelback.crd.ge.com
Fri Jun 28 07:40:16 AEST 1991


In article <160662 at pyramid.pyramid.com>, markhall at pyrps5.pyramid.com
(Mark Hall) writes:
|>>
|>>#define SWAP(a, b) {int c;  c = a; a = b; b = c}
|>
|>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;

#define SWAP(a,b) {int c, *pa=&(a), *pb=&(b); c=*pa; *pa=*pb; *pb=c;}

This doesn't solve all problems, but it gets the one you mentioned. I
think. :-)
                
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com



More information about the Comp.lang.c mailing list