What is strcmp supposed to return if one or both arguments passed to it are NULL ?

R

Richard Heathfield

(e-mail address removed) said:
What is strcmp supposed to return if one or both arguments passed to it
are NULL ?

A demon.

Inspect your handkerchief - carefully.
 
C

Chris Dollin

What is strcmp supposed to return if one or both arguments passed to it
are NULL ?

It isn't.

If either argument to strcmp is null, the behaviour is undefined.

So it may return -1, or 1, or 0, or 17, or 1829. It may generate some
kind of signal. It may exit, increment arbitrary memory, or call
some random one of your functions. Anything: the C standard places
no restrictions on strcmp if either argument is null; there is no
"supposed".

Typically, the behaviour is constrained by other standards and
mechanisms obeyed and performed by your implementation, so you'd
better hope that /they/ are working for you.

Are you willing to bet your nose on it?
 
L

lovecreatesbeauty

What is strcmp supposed to return if one or both arguments passed to it
are NULL ?

It may not be completed and crash midway, thereby having nothing
returned.

lovecreatesbeauty
 
G

goose

What is strcmp supposed to return if one or both arguments passed to it
are NULL ?

Why don't you try it and let us know?

Or read a good C book?
Or read the FAQ for this group (posted everywhere)
Or read the standards document?

....

Or simply tell your instructor that you're
too lazy to be doing this course?

goose,
bad bad bad!!! I *really* should be more
mild on the newsgroups, but sometimes I just
cannot help myself.
 
A

Andrew Poelstra

I read the Standard. It didn't say.

The Standard does say that if the Standard doesn't define something, it
is undefined.

Therefore, this is undefined.
 
K

Keith Thompson

Richard Heathfield said:
(e-mail address removed) said:

If the Standard doesn't define the behaviour, the behaviour is undefined.

And in this case, the Standard states explicitly that the behavior is
undefined.

C99 7.1.4p1:

Each of the following statements applies unless explicitly stated
otherwise in the detailed descriptions that follow: If an argument
to a function has an invalid value (such as a value outside the
domain of the function, or a pointer outside the address space of
the program, or a null pointer, or a pointer to non-modifiable
storage when the corresponding parameter is not const-qualified)
or a type (after promotion) not expected by a function with
variable number of arguments, the behavior is undefined.
[...]
 
R

Richard Heathfield

Keith Thompson said:
Richard Heathfield <[email protected]> writes:

And in this case, the Standard states explicitly that the behavior is
undefined.

C99 7.1.4p1:

Ah, thank you - with that hint, I found it in C89 too. It's in 4.1.6.

(I did buy a copy of C99, but I rarely refer to it because it's in such a
stupid stupid format.)

<snip>
 
G

Gordon Burditt

What is strcmp supposed to return if one or both arguments passed to it
are NULL ?

Fire the author of the code and delete all copies of it (author
*AND* code). This is, however, a quality of implementation issue.
Some of them just fire anyone. Incidentally, did anyone notice
that Bill Gates is stepping down from active management of Microsoft?
Coincidence?

With only a few exceptions (such as strtok()), passing NULL to a
function expecting a string pointer as an argument invokes the wrath
of undefined behavior. The exceptions are documented in the standard.

Gordon L. Burditt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top