By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

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

P: n/a
What is strcmp supposed to return if one or both arguments passed to it
are NULL ?

Jul 13 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
sp****@gmail.com said:
What is strcmp supposed to return if one or both arguments passed to it
are NULL ?
A demon.

Inspect your handkerchief - carefully.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 13 '06 #2

P: n/a
sp****@gmail.com wrote:
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?

--
Chris "one nostril at a time" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Jul 13 '06 #3

P: n/a
sp****@gmail.com wrote:
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

Jul 13 '06 #4

P: n/a
sp****@gmail.com wrote:
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.

Jul 13 '06 #5

P: n/a
"goose" <ru**@webmail.co.zawrites:
sp****@gmail.com wrote:
>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?
Perhaps because trying it won't actually answer the question.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 13 '06 #6

P: n/a

Keith Thompson wrote:
"goose" <ru**@webmail.co.zawrites:
sp****@gmail.com wrote:
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?

Perhaps because trying it won't actually answer the question.
This is turning out to be an embarrassingly bad day for me

goose,

Jul 13 '06 #7

P: n/a
"goose" <ru**@webmail.co.zawrites:
Keith Thompson wrote:
>"goose" <ru**@webmail.co.zawrites:
sp****@gmail.com wrote:
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?

Perhaps because trying it won't actually answer the question.

This is turning out to be an embarrassingly bad day for me
We all have them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 13 '06 #8

P: n/a

goose wrote:
sp****@gmail.com wrote:
What is strcmp supposed to return if one or both arguments passed to it
are NULL ?
Or read the standards document?
I read the Standard. It didn't say.

Jul 13 '06 #9

P: n/a
On 2006-07-13, sp****@gmail.com <sp****@gmail.comwrote:
>
goose wrote:
>sp****@gmail.com wrote:
What is strcmp supposed to return if one or both arguments passed to it
are NULL ?
Or read the standards document?

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.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above domain.
"You people hate mathematics." -- James Harris
Jul 13 '06 #10

P: n/a
sp****@gmail.com said:
goose wrote:
>sp****@gmail.com wrote:
What is strcmp supposed to return if one or both arguments passed to it
are NULL ?
Or read the standards document?

I read the Standard. It didn't say.
If the Standard doesn't define the behaviour, the behaviour is undefined.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 13 '06 #11

P: n/a
Richard Heathfield <in*****@invalid.invalidwrites:
sp****@gmail.com said:
>goose wrote:
>>sp****@gmail.com wrote:
What is strcmp supposed to return if one or both arguments passed to it
are NULL ?
Or read the standards document?

I read the Standard. It didn't say.

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 13 '06 #12

P: n/a
sp****@gmail.com wrote:
>
goose wrote:
sp****@gmail.com wrote:
What is strcmp supposed to return if one or both arguments passed
to it are NULL ?
Or read the standards document?

I read the Standard. It didn't say.
So the behavior is undefined?

Brian
Jul 13 '06 #13

P: n/a
Keith Thompson said:
Richard Heathfield <in*****@invalid.invalidwrites:
<snip>
>>
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:
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>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 13 '06 #14

P: n/a
>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
Jul 14 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.