macros and semicolons

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Sun Jun 30 19:43:19 AEST 1991


In article <1991Jun29.125314.22043 at thunder.mcrcim.mcgill.edu>, mouse at thunder.mcrcim.mcgill.edu (der Mouse) writes:
> In article <6531 at goanna.cs.rmit.oz.au>, ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
> > In article <160662 at pyramid.pyramid.com>, markhall at pyrps5.pyramid.com (Mark Hall) writes:
> >> NO macro will work for a swap.
> 
> >  Er, this turns out not to be the case.  Consider
> > 	#define swap(Type, This, That)				\
> > 	    do {						\
> > 		Type *ThisQZ = &(This), *ThatQZ = &(That), t;	\ 
> > 		t = *ThisQZ, *ThisQZ = *ThatQZ, *ThatQZ = t;	\
> > 	    } while (0)
> 
> More seriously, that won't work if an argument doesn't have an address

That one I can't handle.  The rest of the objections I can meet.

static void swap(size_t size, char *a, char *b)
    {
	char t;
	while (size--) t = *a, *a = *b, *b = t;
    }

#define swap(This, That) (swap)(sizeof (This), (char*)&(This), (char*)&(That))

(I know the function and the macro have the same name.  I may have slipped
up in the _way_ I did it, but ANSI C lets you do it.)

-- 
I agree with Jim Giles about many of the deficiencies of present UNIX.



More information about the Comp.lang.c mailing list