473,395 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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

Jul 13 '06 #1
14 2171
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
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
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
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
"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

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

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
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
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
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
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
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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nobody | last post by:
Lets say I have a class that is only available if a specific DLL (a.dll) is present. I can't link to that DLL through lib files or my app will fail on any machine that doesn't have a.dll. So I...
53
by: Allan Bruce | last post by:
Hi there, I am reading a file into a char array, and I want to find if a string exists in a given line. I cant use strcmp since the line ends with '\n' and not '\0'. Is there a similar function...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
21
by: tyler_durden | last post by:
hi there peeps... like I say in the topic, I need to do an e-mail program in C language, and has to be made until the 3th of january..the problem is I'm having some problems with it. I was...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
12
by: shya | last post by:
hi there! I just wrote a function that reverse a string. It seems all ok to me, but the program goes on segmentation fault on *s = *t_str. Here's the code: #include <stdio.h> #include...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
47
by: fishpond | last post by:
One way I've seen strcmp(char *s1, char *s2) implemented is: return immediately if s1==s2 (equality of pointers); otherwise do the usual thing of searching through the memory at s1 and s2. Of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.