[LCP]wrapper around strcmp

Bill Rausch William.Rausch at numerical.com
Fri May 24 17:11:05 UTC 2002


At 5:51 PM +0530 5/22/02, sasidhar p wrote:
>Hi all,
>  I am interested in a wrapper around 'strcmp' to avoid problems that 
>might occur when one of the agruments to 'strcmp' are NULL. I would 
>like your opinions/advice on the following two approaches in terms 
>of efficiency as I plan keep them in my library routines.
>
>1)  inline bool streq1(const char*a, const char*b) {
>      if (!a || !b || a[0] != b[0])
>        return false;
>      return !strcmp(a,b);
>    }
>
>
>2)  inline bool streq2(const char*a, const char*b) {
>      assert(a != NULL);
>      assert(b != NULL);
>      return !strcmp(a,b);
>    }
>
>Thanks,
>Sasi.

#1 returns FALSE for the NULL pointers.  #2 exits.  There is no 
efficiency comparison to make because they do different things.  You 
choose which behavior you want.

Also, what if both a and b are NULL?  Should the return value be TRUE 
since they are equal?  Maybe that is when you should quit with an 
assert.

Personally, I don't have a problem with the idea of a wrapper 
handling NULLs, but I'd keep the -,0,+ logic of the real strcmp as 
being more generally useful.

-- 
Bill Rausch, Software Development, UNIX, Mac, Windows
Numerical Applications, Richland, WA  509-943-0861




More information about the linuxCprogramming mailing list