Newsgroups: comp.lang.c Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!snorkelwacker.mit.edu!thunder.mcrcim.mcgill.edu!mouse From: mouse@thunder.mcrcim.mcgill.edu (der Mouse) Subject: Re: macros and semicolons Message-ID: <1991Jun29.125314.22043@thunder.mcrcim.mcgill.edu> Organization: McGill Research Centre for Intelligent Machines References: <1991Jun24.213932.595@otago.ac.nz> <160662@pyramid.pyramid.com> <6531@goanna.cs.rmit.oz.au> Date: Sat, 29 Jun 91 12:53:14 GMT Lines: 28 In article <6531@goanna.cs.rmit.oz.au>, ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes: > In article <160662@pyramid.pyramid.com>, markhall@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@larry.mcrcim.mcgill.edu