Generic Swap

Karl Heuer karl at haddock.ima.isc.com
Wed Jun 13 12:07:41 AEST 1990


In article <1990Jun11.214844.21531 at agate.berkeley.edu> dankg at sandstorm.Berkeley.EDU (Dan KoGai) writes:
>void *temp;
>#define swap(type, x, y) temp = (type *)malloc(sizeof(type));\
>	*(type)temp = x; x = y ; y = *(type)temp; free(temp)

Those casts don't make sense.  I think you want
	#define swap(type, x, y) temp = malloc(sizeof(type));\
	*(type *)temp = x; x = y ; y = *(type *)temp; free(temp)

>	Since this is a macro, it should work even though your compiler
>does not allow passing structs as arguments (struct assignment is valid
>since K&R while struct as argument is not).

Wrong.  The three forms of struct copy (assignment, pass by value, return by
value) were all added at the same time, shortly after K&R 1 was published.

Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list