Re: Question about strchr() / strrchr()

Discussion in 'C Programming' started by Martin Dickopp, Aug 16, 2003.

  1. Irrwahn Grausewitz <> writes:

    > Does anybody know the reason why the strchr() and strrchr()
    > functions consider the terminating null character to be part
    > of the string?


    I guess that's just one of two possible ways to define the behavior of these
    functions if the second argument is zero.

    If the terminating null character where not considered a part of the string,
    the functions whould always return a null pointer in this case. That seems
    less useful to me than the current behavior.

    Martin
     
    Martin Dickopp, Aug 16, 2003
    #1
    1. Advertising

  2. Martin Dickopp <> wrote in
    <bhlbbq$j38$01$-online.com>:

    >If the terminating null character where not considered a part of the string,
    >the functions whould always return a null pointer in this case. That seems
    >less useful to me than the current behavior.
    >

    Maybe I'm terribly wrong, but to me it seems that the
    probably most common use for strchr() is to determine
    if a given character is in a set of characters
    provided as the first argument.

    If I write:

    char set[] = "abc";
    int c = <any-character>;
    cp = strchr( set, c );

    I want to know if <c> is one of 'a', 'b' or 'c'. If <c> equals
    '\0' I get a valid pointer, regardless of the contents of <set>,
    as long as it's a valid pointer to a null-terminated character
    string at all. So I have to explicitly check for c == '\0'. If
    strchr would *not* consider the terminating null to be part of
    the string it would give me NULL pointer telling me: "no, <c> is
    *not* one of 'a', 'b' or 'c'." (Much like strchr( "abc", 'd')
    would do.)

    Hm, thinking further, strchr( <somestring>, '\0' ) is an easy way
    to obtain a pointer that points one behind the last non-null char
    in <somestring> - for whatever purpose. So maybe it is intended
    that:

    char cp[] = "some_chars \0 some_more";

    strchr( cp, '\0' ) == cp + strlen( cp ) !!!

    Now that I got myself in a state of complete confusion I hope
    that someone will be so generous to help me get back on track.

    thx
    Irrwahn

    --
    If it's not on fire, it's a software problem.
     
    Irrwahn Grausewitz, Aug 16, 2003
    #2
    1. Advertising

  3. Martin Dickopp

    Kevin Easton Guest

    Irrwahn Grausewitz <> wrote:
    > Kevin Easton <> wrote in
    > <newscache$t2vpjh$tyk$>:
    >
    >>If that's what you want, then just test (c && strchr(set, c)) instead of
    >>(strchr(set, c)).
    >>

    > Right, that seems to be the best way to deal with it.
    >
    > But still my original question /why/ the behaviour of
    > str[r]chr() differs from other str*() functions
    > remains unanswered. I've got severe problems to think
    > of the standardization comitee people as a bunch of
    > sadists putting poor strchr() into Schroedingers box,
    > accepting whatever the result is after a random period
    > of time. :)
    >
    > But maybe there isn't an answer, except: "It is as it
    > is for historical reasons and to retain compatibility
    > with pre-ANSI/pre-ISO C-programs.


    That could be it, but I suspect it's more likely that it's because, if
    the terminating null character wasn't considered part of the string, the
    result would always be NULL (and so there's no point calling strchr to
    find that out); but as it is, the result is different depending on the
    string, and therefore useful.

    - Kevin.
     
    Kevin Easton, Aug 16, 2003
    #3
  4. Irrwahn Grausewitz <> writes:

    > Martin Dickopp <> wrote in
    > <bhlbbq$j38$01$-online.com>:
    >
    > >If the terminating null character where not considered a part of the string,
    > >the functions whould always return a null pointer in this case. That seems
    > >less useful to me than the current behavior.

    >
    > Maybe I'm terribly wrong, but to me it seems that the
    > probably most common use for strchr() is to determine
    > if a given character is in a set of characters
    > provided as the first argument.


    Is it? I can only speak for myself, but I've never used it for that.

    Anyways, what if the set of characters to test against /does/ include the
    null character? If you were in that situation, and `strchr' disregarded the
    null character, you would have likewisely complained. ;-)

    > If I write:
    >
    > char set[] = "abc";
    > int c = <any-character>;
    > cp = strchr( set, c );
    >
    > I want to know if <c> is one of 'a', 'b' or 'c'.


    Why don't you use `memchr' then? Then you can express both cases: '\0' is or
    is not part of the set.

    /* Test if c is 'a', 'b', or 'c'. */
    const char set [] = "abc";
    cp = memchr (set, c, sizeof set - 1);

    /* Test if c is 'a', 'b', 'c', or '\0'. */
    const char set [] = "abc";
    cp = memchr (set, c, sizeof set);

    Martin
     
    Martin Dickopp, Aug 16, 2003
    #4
  5. Martin Dickopp <> wrote in
    <bhlr5s$ies$02$-online.com>:

    >Why don't you use `memchr' then? Then you can express both cases: '\0' is or
    >is not part of the set.
    >
    > /* Test if c is 'a', 'b', or 'c'. */
    > const char set [] = "abc";
    > cp = memchr (set, c, sizeof set - 1);
    >
    > /* Test if c is 'a', 'b', 'c', or '\0'. */
    > const char set [] = "abc";
    > cp = memchr (set, c, sizeof set);
    >


    Believe it or not - I forgot about that memchr() function!!!
    I've been abstinent from C for too long, I guess...

    Thanks to you and Kevin for the hints.

    Irrwahn
    --
    If you don't care where you are, then you ain't lost.
     
    Irrwahn Grausewitz, Aug 16, 2003
    #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,086
    Default User
    Aug 22, 2003
  2. Servé Lau

    a shorter strchr

    Servé Lau, Feb 17, 2004, in forum: C Programming
    Replies:
    9
    Views:
    676
  3. Sean Berry
    Replies:
    10
    Views:
    690
  4. David Warner

    strchr & finding multiple occurances of a char

    David Warner, Apr 25, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,361
    CBFalconer
    Apr 26, 2005
  5. Angus
    Replies:
    8
    Views:
    606
    Kenneth Brody
    Dec 19, 2006
Loading...

Share This Page