SWAP macro

Barry Shein bzs at bu-cs.UUCP
Sat Jun 28 10:45:19 AEST 1986


And then again, there's always this sort of thing (look in 4.2's
/usr/include/sys/mbuf.h for a strong precedent):

#define swap(x,y,t) { \
			t *p1 = &(x), *p2 = &(y), tmp; \
 \
			tmp = *p1; \
			*p1 = *p2; \
			*p2 = tmp; \
		}

which is called with something like:

	swap(x,y,double);

really not a bad thing (or is it too much bother for the programmer to
know what type s/he is working with?)  Still doesn't work with
registers of course (but I don't think typeof() would solve that
problem either.) Also, pointer types would have to be typedef'd to be
useful as swap(x,y,char *) would not quite work (but w/ a typedef
it works fine.)

	-Barry Shein, Boston University



More information about the Comp.lang.c mailing list