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

Discussion in 'C Programming' started by spibou@gmail.com, Jul 13, 2006.

  1. Guest

    What is strcmp supposed to return if one or both arguments passed to it
    are NULL ?
     
    , Jul 13, 2006
    #1
    1. Advertising

  2. 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)
     
    Richard Heathfield, Jul 13, 2006
    #2
    1. Advertising

  3. Chris Dollin Guest

    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/
     
    Chris Dollin, Jul 13, 2006
    #3
  4. 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
     
    lovecreatesbeauty, Jul 13, 2006
    #4
  5. goose Guest

    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.
     
    goose, Jul 13, 2006
    #5
  6. Re: What is strcmp supposed to return if one or both argumentspassed to it are NULL ?

    "goose" <> writes:
    > 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) <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.
     
    Keith Thompson, Jul 13, 2006
    #6
  7. goose Guest

    Keith Thompson wrote:
    > "goose" <> writes:
    > > 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,
     
    goose, Jul 13, 2006
    #7
  8. Re: What is strcmp supposed to return if one or both argumentspassed to it are NULL ?

    "goose" <> writes:
    > Keith Thompson wrote:
    >> "goose" <> writes:
    >> > 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) <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.
     
    Keith Thompson, Jul 13, 2006
    #8
  9. Guest

    goose wrote:

    > 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, 2006
    #9
  10. On 2006-07-13, <> wrote:
    >
    > goose wrote:
    >
    >> 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
     
    Andrew Poelstra, Jul 13, 2006
    #10
  11. said:
    > goose wrote:
    >> 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)
     
    Richard Heathfield, Jul 13, 2006
    #11
  12. Re: What is strcmp supposed to return if one or both argumentspassed to it are NULL ?

    Richard Heathfield <> writes:
    > said:
    >> goose wrote:
    >>> 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) <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.
     
    Keith Thompson, Jul 13, 2006
    #12
  13. Default User Guest

    wrote:

    >
    > goose wrote:
    >
    > > 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
     
    Default User, Jul 13, 2006
    #13
  14. Keith Thompson said:

    > Richard Heathfield <> writes:

    <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)
     
    Richard Heathfield, Jul 13, 2006
    #14
  15. >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
     
    Gordon Burditt, Jul 14, 2006
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl
    Replies:
    21
    Views:
    1,025
    Patricia Shanahan
    Aug 24, 2006
  2. Fred L. Kleinschmidt

    NULL argument to strcmp

    Fred L. Kleinschmidt, Dec 15, 2004, in forum: C Programming
    Replies:
    15
    Views:
    1,370
    Keith Thompson
    Dec 17, 2004
  3. Steven
    Replies:
    9
    Views:
    417
    Keith Thompson
    Dec 29, 2005
  4. aneuryzma
    Replies:
    3
    Views:
    780
    Jim Langston
    Jun 16, 2008
  5. KevinSimonson
    Replies:
    1
    Views:
    846
    Daniele Futtorovic
    Mar 13, 2011
Loading...

Share This Page