Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!hao!umigw!steve From: steve@umigw.MIAMI.EDU (steve emmerson) Newsgroups: comp.lang.c Subject: Re: typeof isn't enough to define swap correctly Message-ID: <109@umigw.MIAMI.EDU> Date: Fri, 4-Sep-87 19:22:39 EDT Article-I.D.: umigw.109 Posted: Fri Sep 4 19:22:39 1987 Date-Received: Sat, 5-Sep-87 21:33:15 EDT References: <557@rocky.STANFORD.EDU> <1880@sol.ARPA> Reply-To: steve@umigw.UUCP (steve emmerson) Organization: University of Miami Lines: 23 Summary: close but no cigar In article <1880@sol.ARPA> crowl@cs.rochester.edu (Lawrence Crowl) writes: >The following swap macro is correct. It relies on the pointer features of C >to evaluate its arguments exactly once. > >#define swap(a,b) { \ > typeof(a) t,*p1,*p2; /* temporary and pointers to sources */ \ > p1 = &(a); p2 = &(b); /* evaluate arguments exactly once */ \ > t = *p1; *p1 = *p2; *p2 = t; /* do actual swap */ \ >} >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*. Close but no cigar. ------------------------------------------------------------------------------- Steve Emmerson USAN: steve@umigw RSMAS Remote Sensing Laboratory AT&T: (305) 361-4065 University of Miami SPAN: miami::emmerson (host 3.2) UUCP: ...!hao!umigw!miami!emmerson USPS: RSMAS/MPO DDN: emmerson@miami.miami.edu 4600 Rickenbacker Cswy. emmerson%miami.span@star.stanford.edu Miami, FL 33149-1098 emmerson%miami.span@vlsi.jpl.nasa.gov