strchr with its second argument = 0

Discussion in 'C Programming' started by Thomas Richter, May 4, 2012.

  1. Hi folks,

    what is the behavior of strchr if the second argument (the character to
    be searched for) is set to zero? Does it find the end of the string, or
    does it find nothing and should it return NULL?

    Thanks,
    Thomas
     
    Thomas Richter, May 4, 2012
    #1
    1. Advertising

  2. Thomas Richter <-berlin.de> wrote:

    > what is the behavior of strchr if the second argument (the
    > character to be searched for) is set to zero? Does it find the
    > end of the string, or does it find nothing and should it return
    > NULL?


    The definition says:

    | The strchr function locates the first occurrence of c (converted
    | to a char) in the string pointed to by s. The terminating null
    | character is considered to be part of the string.

    So it should return a pointer to the end of the string.

    -- Ralf
     
    Ralf Damaschke, May 4, 2012
    #2
    1. Advertising

  3. Thomas Richter

    Kaz Kylheku Guest

    On 2012-05-04, Thomas Richter <-berlin.de> wrote:
    > Hi folks,
    >
    > what is the behavior of strchr if the second argument (the character to
    > be searched for) is set to zero? Does it find the end of the string, or
    > does it find nothing and should it return NULL?


    It should find nothing and return null. The null character isn't a character
    which is in the string. By definition of a C string, a C string cannot contain
    the null character.

    --
    If you ever need any coding done, I'm your goto man!
     
    Kaz Kylheku, May 4, 2012
    #3
  4. Thomas Richter

    Kaz Kylheku Guest

    On 2012-05-04, Kaz Kylheku <> wrote:
    > On 2012-05-04, Thomas Richter <-berlin.de> wrote:
    >> Hi folks,
    >>
    >> what is the behavior of strchr if the second argument (the character to
    >> be searched for) is set to zero? Does it find the end of the string, or
    >> does it find nothing and should it return NULL?

    >
    > It should find nothing and return null. The null character isn't a character
    > which is in the string. By definition of a C string, a C string cannot contain
    > the null character.


    Sorry; that's wrong! I was staring at the definition in C99, and somehow read
    that as "the terminating null character is *not* considered part of the
    string"; but the word "not" is not there.

    (Of course; why would such text even be included other than to give a special
    exception to the definition of a string.)

    Still, this text actually poor. "Part of the string": does that mean part of
    the representation of the string, or part of the string data? Of course a null
    is part of every correct C string. But it's not considered part of the abstract
    string. The intent is clear though.

    --
    If you ever need any coding done, I'm your goto man!
     
    Kaz Kylheku, May 4, 2012
    #4
  5. Thomas Richter

    Kaz Kylheku Guest

    On 2012-05-04, pete <> wrote:
    > Kaz Kylheku wrote:
    >>
    >> On 2012-05-04, Kaz Kylheku <> wrote:

    >
    >> > The null character isn't a character
    >> > which is in the string. By definition of a C string,
    >> > a C string cannot contain
    >> > the null character.

    >>
    >> Sorry; that's wrong! I was staring at the definition in C99,
    >> and somehow read
    >> that as "the terminating null character is *not* considered
    >> part of the string"; but the word "not" is not there.
    >>
    >> (Of course; why would such text even be included
    >> other than to give a special
    >> exception to the definition of a string.)

    >
    > Such text in the definition of strchr, would be included
    > to remind you of how a string is actually defined.
    >
    > ISO/IEC 9899:1999 (E)
    > 7. Library
    > 7.1 Introduction
    > 7.1.1 Definitions of terms
    > 1 A string is a contiguous sequence of characters
    > terminated by
    > and including the first null character.


    That is true of the representation of a string. To store a four character
    string we have to allocate five bytes and copy five bytes, after all. If we do
    not reserve that space, or ensure that the null is propagated somehow, then we
    do not have a string object.

    But, on the other hand, strlen("abc") is 3. Only those characters prior to the
    nul are counted, and that quantity is called the length of the string.
    Something outside of the measured length of an object is not part of that
    object. Or at best it is ambiguous. (For instance, the height of some building,
    for some given purpose, may or may not exclude an antenna mast on top.)

    If strings had a header indicating their length, followed by the data then
    7.1.1 would have define a string as being that header and that data.

    It is highly implausible that the text in strchr is given for the purpose of
    reminding the reader what a string is. It is to give a requirement of behavior
    which only comes into play when the search character is null.

    In fact, this kind of text is given in quite a few places. In contexts where it
    may not be clear, there is a reminder "including the terminating null
    character". Furthermore, in a few places, there is the opposite reminder: "not
    including the terminating null character". So, which of those places are
    the reminders about how a string is defined, and which are the exceptions?

    What these are are reminders of the definition of a string is two-fold and
    ambiguous: it is either the abstract sequence of characters, or the storage
    object, depending on context.

    Consider stpbrk. Its definition has no reminder. What should strpbrk("", "")
    return? If 7.1.1 gives the definition of an abstract string, then "" must be
    considered to be the string consisting of the null character. The "" passed as
    a right argument must denote a set which contains the null character, and since
    that character is found in the left argument, a pointer should be returned
    to the left argument's null. But, in fact, the return value in this case is
    the null pointer. An empty string denotes an empty set, and a string in which
    no character can be found.

    Likewise, if that text about the null character were not given in the
    definition of strchr, the interpretation would have to be that the null
    character is never found, and so a null pointer is returned. A null character
    normally cannot be found in a string because the search terminates when the
    terminator is encountered. Null-finding behavior is an exception.
     
    Kaz Kylheku, May 5, 2012
    #5
    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. Replies:
    32
    Views:
    7,223
    Default User
    Aug 22, 2003
  2. Martin Dickopp

    Re: Question about strchr() / strrchr()

    Martin Dickopp, Aug 16, 2003, in forum: C Programming
    Replies:
    4
    Views:
    2,306
    Irrwahn Grausewitz
    Aug 16, 2003
  3. thunk
    Replies:
    1
    Views:
    363
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    539
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    664
    thunk
    Apr 3, 2010
Loading...

Share This Page