Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wasatch!cs.utexas.edu!uunet!mcvax!hp4nl!philapd!ssp1!roelof From: roelof@idca.tds.PHILIPS.nl (R. Vuurboom) Newsgroups: comp.lang.c Subject: Re: swap(x,y) Message-ID: <228@ssp1.idca.tds.philips.nl> Date: 22 Aug 89 17:08:11 GMT References: Organization: Philips Telecommunication and Data Systems, The Netherlands Lines: 42 In article tg1e+@andrew.cmu.edu (Timothy R. Gottschalk) writes: > > To swap two variables x,y in C without using a temporary variable: > > /* begin swap */ > x += y; > y = x - y; > x -= y; > /* end swap */ > > Just curious...(this looks pretty valid) does anyone know an even >simpler method? I would never program this way -- it's more of a theory >question. I've been told that it can't be done (???). ^^^ hey ma, thars one o dem trigraphs again...:-) This algorithm caught me out once. Try the following: #define swap(x,y) { x += y; \ y = x-y; \ x -= y; } main() { int i = 3; int j = 4; swap(i,j); printf ("i = %d j = %d\n",i,j); i = 3; swap(i,i); printf ("i = %d\n",i); } -- I don't know what the question means, but the answer is yes... KLM - Koninklijke Luchtvaart Maatschappij => coneenclicker lughtfart matscarpie Roelof Vuurboom SSP/V3 Philips TDS Apeldoorn, The Netherlands +31 55 432226 domain: roelof@idca.tds.philips.nl uucp: ...!mcvax!philapd!roelof