Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: macros and semicolons Message-ID: <6567@goanna.cs.rmit.oz.au> Date: 30 Jun 91 09:43:19 GMT References: <1991Jun24.213932.595@otago.ac.nz> <160662@pyramid.pyramid.com> <1991Jun29.125314.22043@thunder.mcrcim.mcgill.edu> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 29 In article <1991Jun29.125314.22043@thunder.mcrcim.mcgill.edu>, mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes: > 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) > > More seriously, that won't work if an argument doesn't have an address That one I can't handle. The rest of the objections I can meet. static void swap(size_t size, char *a, char *b) { char t; while (size--) t = *a, *a = *b, *b = t; } #define swap(This, That) (swap)(sizeof (This), (char*)&(This), (char*)&(That)) (I know the function and the macro have the same name. I may have slipped up in the _way_ I did it, but ANSI C lets you do it.) -- I agree with Jim Giles about many of the deficiencies of present UNIX.