Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!harvard!seismo!hao!noao!terak!anasazi!steve From: steve@anasazi.UUCP (Steve Villee) Newsgroups: net.lang.c Subject: Re: swap() macro Message-ID: <370@anasazi.UUCP> Date: Fri, 20-Jun-86 13:44:48 EDT Article-I.D.: anasazi.370 Posted: Fri Jun 20 13:44:48 1986 Date-Received: Mon, 23-Jun-86 03:02:19 EDT References: <1201@brl-smoke.ARPA> Organization: Anasazi, Phoenix Az. Lines: 41 > Can someone construct a version that makes > > int *a, *b; > swap (*a++,*b++); > > work right? > > Paul > Schauble at MIT-Multics.arpa -------------------------------- #define swap(x, y) \ if (1) \ { \ register int *xp, *yp, t; \ xp = &(x); \ yp = &(y); \ t = *xp; \ *xp = *yp; \ *yp = t; \ } \ else -------------------------------- The "if (1) ... else" allows the user to put a semicolon after the macro invocation, and makes code such as if (today == tuesday) swap(*a++, *b++); else swap(*c++, *d++); work correctly. --- Steve Villee (ihnp4!mot!anasazi!steve) International Anasazi, Inc. 7500 North Dreamy Draw Drive, Suite 120 Phoenix, Arizona 85020 (602) 870-3330