Is this swap() macro correct?

Richard H. Gumpertz rhg at cpsolv.CPS.COM
Fri Jan 19 10:18:13 AEST 1990


In article <21068 at stealth.acf.nyu.edu> brnstnd at stealth.acf.nyu.edu (Dan Bernstein) writes:
>Say swap() is defined as
>
>#define block do {
>#define endblock } while(0)
>#define swap(x,y,typ) block typ *_MV_x = x; typ *_MV_y = y; typ tmp;\
>                    tmp = *_MV_x; *_MV_x = *_MV_y; *_MV_y = tmp; endblock
>
>Now constructions like swap(f++,g++,real) work and are faster than a

Stylistic nit: I would make it &(x) and &(y) in the first line of swap and
make typ the first parameter and then make the call you show be
	swap(real, *f++, *g++)
Maybe I am strange, but I find that easier to read.
-- 
  ==========================================================================
  | Richard H. Gumpertz    rhg at CPS.COM    (913) 642-1777 or (816) 891-3561 |
  | Computer Problem Solving, 8905 Mohawk Lane, Leawood, Kansas 66206-1749 |
  ==========================================================================



More information about the Comp.lang.c mailing list