Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: typeof isn't enough to define swap correctly Message-ID: <8415@mimsy.UUCP> Date: Fri, 4-Sep-87 19:08:57 EDT Article-I.D.: mimsy.8415 Posted: Fri Sep 4 19:08:57 1987 Date-Received: Sat, 5-Sep-87 20:57:13 EDT References: <557@rocky.STANFORD.EDU> <1880@sol.ARPA> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 19 This should work even without typeof(). I do not recommend it. Note that you will get warnings from PCC if SWAP is applied to arrays. You can remove the sizeof/panic code if you dare. #define SWAP(a, b) { \ struct __t { char bytes[sizeof(a)]; }; \ struct __t *__a = (struct __t *)&(a); \ struct __t *__b = (struct __t *)&(b); \ struct __t __t; \ if (sizeof(struct __t) != sizeof(b)) \ panic("bad swap() args: %s, line %d", \ __FILE__, __LINE__); \ __t = *__a; \ *__a = *__b; \ *__b = __t; \ } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris