[LCP]wrapper around strcmp

Mike & Penny Novack stepbystepfarm at shaysnet.com
Thu May 23 02:34:06 UTC 2002


OK Sasi, the FIRST thing you want to do is decide what is the INTENDED
behavior of your modified "strcmp".

Do you want it to hang the program? No? Then don't use your second
version because that's what it will do (if either of the strings is
NULL). That's the purpose of assertions, easy debugging of exceptional
conditions you expect never to exist and which your code cannot handle.

How about the first version? Well maybe you want to think more clearly
about what you REALLY want your version of strcmp to do. Maybe you do
want it to return false if BOTH strings are NULL (did you consider that
possibility and whether you might in that case prefer to return "true").

You didn't indicate how you intended to "keep" these. Did you consider
other alternatives to an "inline function"? I perhaps would have chosen
a "macro" whose body was a "conditional expression (or nested
conditional expression if I were handling "both NULL" as true).

Michael



----- Original Message -----
From: sasidhar p <psasidhar at hotmail.com>
To: <linuxcprogramming at lists.linux.org.au>
Sent: Wednesday, May 22, 2002 8:21 AM
Subject: [LCP]wrapper around strcmp


> 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.






More information about the linuxCprogramming mailing list