swap() macro

Tainter tainter at ihlpg.UUCP
Thu Jul 3 11:20:16 AEST 1986


> In article <1836 at brl-smoke.ARPA> gwyn at brl.arpa (Doug Gwyn
> (VLD/VMB) <gwyn>) 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 at umcp-cs		ARPA:	chris at 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



More information about the Comp.lang.c mailing list