swap()
gof at nosc.ARPA
gof at nosc.ARPA
Thu Jun 26 08:58:28 AEST 1986
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 at noscvax.ARPA)
More information about the Comp.lang.c
mailing list