Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!caip!topaz!harvard!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.lang.c Subject: Re: swap() macro Message-ID: <2226@umcp-cs.UUCP> Date: Wed, 2-Jul-86 00:51:40 EDT Article-I.D.: umcp-cs.2226 Posted: Wed Jul 2 00:51:40 1986 Date-Received: Thu, 3-Jul-86 01:14:58 EDT References: <1201@brl-smoke.ARPA> <193@stracs.cs.strath.ac.uk> <733@ho95e.UUCP> <1836@brl-smoke.ARPA> <2225@umcp-cs.UUCP> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 68 In article <1836@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) wrote: >>It may be amusing and/or instructive to contemplate the fact that >>there is no way to write a function that exchanges the contents of >>two variables in a language where parameters are passed "by name". In article <2225@umcp-cs.UUCP> I replied: >I have here a C program that effects call-by-name and does >indeed perform a swap: What I failed to consider, of course, is the classic problem with call by name: arrays. Watch what happens when I `swap' `i' and `a[i]', using an expanded form of the swap function (this is necessary to avoid compiler dependencies in this particular case). Ah well, at least I caught my error myself. . . . swap(f1, f2) int *(*f1)(), *(*f2)(); { int t1, t2; t1 = *(*f1)(); t2 = *(*f2)(); *(*f1)() = t2; *(*f2)() = t1; } int a[10], i; int * addr_asubi() { return (&a[i]); } int * addr_i() { return (&i); } /*ARGSUSED*/ main(argc, argv) int argc; char **argv; { a[3] = 6; a[6] = 1; i = 3; swap(addr_i, addr_asubi); /* * Want i to become 6 and a[3] to become 3, with a[6] left * undisturbed. That is not what happens. Figure out what * it will in fact do, before you run it. */ printf("`should' be 6, 3, 1: i = %d, a[3], a[6] = %d\n", i, a[3], a[6]); exit(0); } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516) UUCP: seismo!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@mimsy.umd.edu