Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!whuxl!whuxlm!akgua!gatech!seismo!harvard!bu-cs!bzs From: bzs@bu-cs.UUCP Newsgroups: net.lang.c Subject: SWAP macro (utter insanity+interesting question) Message-ID: <844@bu-cs.UUCP> Date: Wed, 25-Jun-86 01:47:13 EDT Article-I.D.: bu-cs.844 Posted: Wed Jun 25 01:47:13 1986 Date-Received: Sat, 28-Jun-86 07:34:24 EDT Organization: Boston Univ Comp. Sci. Lines: 83 The attached bit of total brain-damage seems to do the job in YAW (yet another way.) I don't actually proffer it as a solution that you might use, no way, don't bother...BUT Interesting question: What is the semantics of: sizeof(*ip++) ?? Try it before you guess, I was shocked (late entry for the obfuscated C contest??) The semantics I found are essential for my solution. P.S. It compiled and ran correctly on (code follows): Hardware OS Software Notes -------- --- -------- --------------- SUN3/180 UNIX4.2 R3.0 PCC DEC2060 TOPS-20 5.4 MIT/PCC provided bcopy() ATT/3B5 UNIX SYSVR2 C used memcpy() IBM3090/200 VPS/VM PCC provided bcopy() Encore/MultiMax UNIX 4.2 PCC VAX/750 UNIX 4.2 PCC VAX/780 VMS4.3 VMS/C provided bcopy() Celerity/1200 UNIX 4.2 PCC DG MV/10000 AOS/VS 6.03 MVUX 2.01/C used memcpy() I also tried it on the SUN, DEC2060, IBM3090, MV10000 and VAX750 with double's instead of ints and it compiled and worked fine, I don't have the energy right now to go through all the others, requests accepted. It also passes lint. YOW, am I portable yet? -Barry Shein, Boston University ____________ #include #define swap(x,y) \ { \ char t1[sizeof(x)], t2[sizeof(t1)]; \ char *p, *q; \ \ p = (char *) &(x); \ q = (char *) &(y); \ bcopy(p,t1,sizeof(t1)); \ bcopy(q,t2,sizeof(t1)); \ bcopy(t2,p,sizeof(t1)); \ bcopy(t1,q,sizeof(t1)); \ } main(argc,argv) int argc; char **argv; { int ai[2], *ip = ai; int bi[2], *ip2 = bi; ai[0] = 1; ai[1] = 2; bi[0] = 3; bi[1] = 4; swap(*ip++,*ip2++); printf("ai[0] = %d *ip = %d bi[0] = %d *ip2 = %d\n", ai[0], *ip, bi[0], *ip2); exit(0); } Should print out something like: ai[0] = 3 *ip = 2 bi[0] = 1 *ip2 = 4 If I said I used memcpy() I added: #define bcopy(x,y,z) memcpy(y,x,z) If I said I provided bcopy() I added: bcopy(from,to,n) char *from, *to; int n; { while(n--) *to++ = *from++; }