Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!gatech!akgua!akguc!mtune!mtuxo!houxm!ihnp4!ihlpg!tainter From: tainter@ihlpg.UUCP (Tainter) Newsgroups: net.lang.c Subject: Re: swap() macro Message-ID: <2140@ihlpg.UUCP> Date: Wed, 2-Jul-86 21:20:16 EDT Article-I.D.: ihlpg.2140 Posted: Wed Jul 2 21:20:16 1986 Date-Received: Sat, 5-Jul-86 05:13:15 EDT References: <1201@brl-smoke.ARPA> <193@stracs.cs.strath.ac.uk> <733@ho95e.UUCP> <1836@brl-smoke.ARPA> <2225@umcp-cs.UUCP> Organization: AT&T Bell Laboratories Lines: 55 > In article <1836@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn > (VLD/VMB) ) writes: > >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". > > How so? It seems rather simple. I have here a C program that effects > call-by-name and does indeed perform a swap: > [Original program deleted for brevity] > 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 Now lets show that it doesn't work: swap(f1, f2) int *(*f1)(), *(*f2)(); { int t; t = *(*f1)(); *(*f1)() = *(*f2)(); *(*f2)() = t; } int c[4], d; int * addr_c_sub_d() { return(&c[d]); } int * addr_d() { return (&d); } /*ARGSUSED*/ main(argc, argv) int argc; char **argv; { d = 2; c[0] = 3; c[1] = 3; c[2] = 1; c[3] = 3; swap(addr_d,addr_c_sub_d); printf("should be 1, 2: d = %d, c[2] = %d\n", d, c[2]); exit(0); } --j.a.tainter