Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!caip!brl-adm!brl-smoke!smoke!pnet01!gof@nosc.ARPA From: gof@nosc.ARPA Newsgroups: net.lang.c Subject: swap() Message-ID: <1668@brl-smoke.ARPA> Date: Wed, 25-Jun-86 18:58:28 EDT Article-I.D.: brl-smok.1668 Posted: Wed Jun 25 18:58:28 1986 Date-Received: Mon, 30-Jun-86 05:15:18 EDT Sender: news@brl-smoke.ARPA Lines: 37 A pair of easy swap(x,y) routines that uses *No* temporary storage can be seen below: #define swap(x,y) (y^=(x^=(y^=x))) int swap_all(x,y,size) char *x,*y; int size; { int count; for (count = 0 ; count < size ;count++) swap(x[count],y[count]); return(count); } It works quickly and, if used with the function, can swap any size or type data area. For simple int or long types the macro will work fine as long as you avoid the usual problems such as swap(a++,y++) or anything similar. A normal call to the function to swap two arrays might be: float first[100], second[100]; . . . swap_all(first,second,sizeof(first)); . . It is a good idea that the sizeof() argument be the smaller of the two areas to avoid swapping to much data. --------------------------------------------------------------------- Jerry Fountain (crash!gof@noscvax.ARPA)