compare strings, strcmp

Peter da Silva peter at ficc.uu.net
Sat Nov 18 04:37:53 AEST 1989


> >>#define  StrEq( a, b )  (*(a) == *(b) && strcmp( a, b ) == 0)	/* UNSAFE */

> > Why the UNSAFE comment?  This looks like utterly standard C to me...

Because !a! and !b! may be evaluated multiple times. What if you do something
like !StrEq(*++argv, "poodle-hair");!? If !*++argv=='p'!, then it will
evaluate !++argv! again.

> #define  StrEq(a,b)  ((*(a) == *(b))?strcmp(a,b):*(a)-*(b))

This has the advantage that it will return the same result as strcmp, but
is otherwise as unsafe (albeit a little more predictable, since it will
always evaluate both arguments twice).
-- 
`-_-' Peter da Silva <peter at ficc.uu.net> <peter at sugar.hackercorp.com>.
 'U`  --------------  +1 713 274 5180.
"vi is bad because it didn't work after I put jelly in my keyboard."
   -- Jeffrey W Percival (jwp at larry.sal.wisc.edu)



More information about the Comp.lang.c mailing list