Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strcmp doesn't accept NULL argument

32 views
Skip to first unread message

Maurice

unread,
Feb 21, 2008, 2:48:24 PM2/21/08
to
Hello:

I noticed that with version 5.5 "strcmp" doesn't accept argument if it is
NULL, it report error problem, is there another equivalent function??

the following program cause error if launched without any argument, if
compiled with version 5.2 or older, it works

Thanks
Maurice

////////////////////////////////////////////////////////////////////
#include "stdio.h"
#include "string.h"

main(int argc, char *argv[])
{
if(!strcmp(argv[1],"hello")) printf(" hi\n");
else printf("bye\n");
}

////////////////////////////////////////////////////////////////////

Thomas Maeder [TeamB]

unread,
Feb 21, 2008, 5:23:06 PM2/21/08
to
"Maurice" <mori...@yahoo.fr> writes:

> I noticed that with version 5.5 "strcmp" doesn't accept argument if
> it is NULL, it report error problem,

Correct. invoking strcmp() with one or two NULL pointers causes the
program to have undefined behavior.


> is there another equivalent function??

Well, you can always write one of your own.


> the following program cause error if launched without any argument, if
> compiled with version 5.2 or older, it works
>
> Thanks
> Maurice
>
> ////////////////////////////////////////////////////////////////////
> #include "stdio.h"
> #include "string.h"
>
> main(int argc, char *argv[])
> {
> if(!strcmp(argv[1],"hello")) printf(" hi\n");
> else printf("bye\n");
> }

The obvious solution is to check argc first.

Ed Mulroy [TeamB]

unread,
Feb 21, 2008, 5:57:42 PM2/21/08
to
strcmp has always been defined in the language specs as not being safe with
a NULL argument.

If you must use a NULL argument then use the Windows function lstrcmp.

. Ed

> Maurice wrote in message
> news:47bd...@newsgroups.borland.com...

Maurice

unread,
Feb 23, 2008, 5:01:11 AM2/23/08
to
"Ed Mulroy [TeamB]" <dont_e...@bitbuc.ket> wrote in message
news:47be01ea$1...@newsgroups.borland.com...

> strcmp has always been defined in the language specs as not being safe
> with a NULL argument.
>
> If you must use a NULL argument then use the Windows function lstrcmp.
>
> . Ed
>

Thank you
Maurice

0 new messages