Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!munnari.oz.au!bruce!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: macros and semicolons Message-ID: <6531@goanna.cs.rmit.oz.au> Date: 27 Jun 91 05:45:09 GMT References: <1991Jun24.213932.595@otago.ac.nz> <160662@pyramid.pyramid.com> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 24 In article <160662@pyramid.pyramid.com>, markhall@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.