Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!bellcore!faline!ulysses!sfmag!sfsup!mpl From: mpl@sfsup.UUCP (M.P.Lindner) Newsgroups: comp.lang.c Subject: Re: typeof isn't enough to define swap correctly Message-ID: <2019@sfsup.UUCP> Date: Wed, 9-Sep-87 20:07:52 EDT Article-I.D.: sfsup.2019 Posted: Wed Sep 9 20:07:52 1987 Date-Received: Sat, 12-Sep-87 00:36:51 EDT References: <557@rocky.STANFORD.EDU> <1880@sol.ARPA> <109@umigw.MIAMI.EDU> Organization: AT&T-IS, Summit N.J. USA Lines: 26 Summary: typeof isn't necessary In article <109@umigw.MIAMI.EDU>, steve@umigw.MIAMI.EDU (steve emmerson) writes: > In article <1880@sol.ARPA> crowl@cs.rochester.edu (Lawrence Crowl) writes: ... > >#define swap(a,b) { \ ... > >} > >One problem still remains. What if typeof(a) != typeof(b)? > > It also won't work with pure array objects (i.e. swapping one array with > another). A generic routine should be able to work with *anything*. Here's one that works WITHOUT TYPEOF AND WORKS FOR ARRAYS! #define swap(a, b) { \ register int i; \ for (i = 0; i < sizeof(a); i++) { \ ((char *) &a)[i] ^= ((char *) &b)[i]; \ ((char *) &b)[i] ^= ((char *) &a)[i]; \ ((char *) &a)[i] ^= ((char *) &b)[i]; \ } \ } OK, so it's not as pretty or efficient as everyone would like, but it's completely general purpose and doesn't need any new features. Mike Lindner