Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!cica!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!brutus.cs.uiuc.edu!apple!oliveb!amdahl!rtech!mikes@rtech.UUCP From: mikes@rtech.UUCP (Mike Schilling) Newsgroups: comp.lang.c Subject: Re: Is this swap() macro correct? Message-ID: <4514@rtech.rtech.com> Date: 18 Jan 90 17:15:04 GMT References: <1990Jan18.002842.441@aqdata.uucp> Sender: news@rtech.rtech.com Distribution: usa Lines: 24 From article <1990Jan18.002842.441@aqdata.uucp>, by sullivan@aqdata.uucp (Michael T. Sullivan): > From article <21068@stealth.acf.nyu.edu>, by brnstnd@stealth.acf.nyu.edu: >> Say swap() is defined as >> >> #define block do { >> #define endblock } while(0) >> #define swap(x,y,typ) block typ *_MV_x = x; typ *_MV_y = y; typ tmp;\ >> tmp = *_MV_x; *_MV_x = *_MV_y; *_MV_y = tmp; endblock > > Why are "block" and "endblock" even bothered with here. Why not just > put the "do {" and "} while (0)" in the definition of swap? Could > somebody please enlighten me. > -- 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?