Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!helios!stat!john From: john@stat.tamu.edu (John S. Price) Newsgroups: comp.lang.c Subject: Re: Is this swap() macro correct? Message-ID: <4103@helios.TAMU.EDU> Date: 18 Jan 90 22:00:49 GMT References: <1990Jan18.002842.441@aqdata.uucp> <4514@rtech.rtech.com> Sender: usenet@helios.TAMU.EDU Reply-To: john@stat.tamu.edu (John S. Price) Distribution: usa Organization: Statistics Department, Texas A&M University Lines: 41 In article <4514@rtech.rtech.com> mikes@rtech.UUCP (Mike Schilling) writes: >In fact why use the do-while at all, and why use pointers? The classic swap >macro is > ># define swap(x, y, typ) \ > { \ > typ tmp; \ > \ > tmp = y; y = x; x = tmp; \ > } > >Is this just as good, or am I being *exceptionally* dense today? The only problem I see with this, and it's not much of a problem, is that if you type swap(x,y,int); that will be expanded to { int tmp; tmp = y; y = x; x = tmp; }; with a semicolon at the end of the block, which isn't always wanted. The reason for the do { ... } while (0) is so that the semicolon can be put there without any problem. Most compilers just see the ; as a null statement and ignore it, anyway, but style wise it's not wanted. The do {...} while (0) loop is usually recognized by the compiler as a useless block, and optimized out, so that it doesn't do the comparison before it exits the block. It will see the 0 as always false, and optimize this out. -------------------------------------------------------------------------- John Price | Morals define our path through life, john@stat.tamu.edu | And everyone's path is different... - Me --------------------------------------------------------------------------