Is NULL a valid parameter for the strcmp() function according to the C
standard?

I found the following code and I have doubts that it is valid

char *parm = NULL;
....
if (strcmp(parm, NULL))
bla,bla,bla

I think that it would be simpler and more correctly to write

if ( parm == NULL )
bla,bla,bla

MS Visual C++ 2005 EE abnormally terminates at runtime if the original if
statement is
used. However if the code is compiled with the IBM C/C++ compiler it runs
without terminating.

Vladimir Grigoriev

Re: Is NULL a valid parameter for the strcmp()? by Victor

Victor
Tue Jul 22 12:15:24 CDT 2008

Vladimir Grigoriev wrote:
> Is NULL a valid parameter for the strcmp() function according to the C
> standard?

No. It has undefined behaviour if either of the arguments does not
point to a valid C string.

> I found the following code and I have doubts that it is valid

It isn't.

>
> char *parm = NULL;
> ....
> if (strcmp(parm, NULL))
> bla,bla,bla
>
> I think that it would be simpler and more correctly to write
>
> if ( parm == NULL )
> bla,bla,bla
>
> MS Visual C++ 2005 EE abnormally terminates at runtime if the original if
> statement is
> used. However if the code is compiled with the IBM C/C++ compiler it runs
> without terminating.

When the behaviour is undefined, anything can happen :-)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Re: Is NULL a valid parameter for the strcmp()? by Vladimir

Vladimir
Tue Jul 22 13:07:01 CDT 2008


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:g654jd$n6c$1@news.datemas.de...
> Vladimir Grigoriev wrote:
>> Is NULL a valid parameter for the strcmp() function according to the C
>> standard?
>
> No. It has undefined behaviour if either of the arguments does not point
> to a valid C string.
>
>> I found the following code and I have doubts that it is valid
>
> It isn't.
>
>>
>> char *parm = NULL;
>> ....
>> if (strcmp(parm, NULL))
>> bla,bla,bla
>>
>> I think that it would be simpler and more correctly to write
>>
>> if ( parm == NULL )
>> bla,bla,bla
>>
>> MS Visual C++ 2005 EE abnormally terminates at runtime if the original if
>> statement is
>> used. However if the code is compiled with the IBM C/C++ compiler it runs
>> without terminating.
>
> When the behaviour is undefined, anything can happen :-)
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask

Thanks

Vladimir Grigoriev