swap() macro

Allan Black allan at cs.strath.ac.uk
Sat Jun 21 03:20:22 AEST 1986


In article <1201 at brl-smoke.ARPA> Schauble at MIT-MULTICS.ARPA writes:
>Can someone construct a version that makes
>    int *a, *b;
>    swap (*a++,*b++);
>work right?

#define swap(p,q) {int *x = &(p), *y = &(q); int z = *x; *x = *y; *y = z;}

This will only work for type 'int'; if you want to make it more general, try
something like:
{long *x = (long)(&(p)) etc.
But be careful with something like that - there's no typechecking, so you
can easily mix pointers up, with disastrous results!

-- 
UUCP:	...!seismo!mcvax!ukc!strath-cs!allan
DARPA:	allan%cs.strath.ac.uk at ucl-cs	JANET:	allan at uk.ac.strath.cs

"This doesn't seem to be working, sir!"
"Yes, but I was trying to ignore it so that YOU wouldn't look stupid!"
				-- Monty Python's Flying Circus



More information about the Comp.lang.c mailing list