possible undefined use of strchr

Discussion in 'C Programming' started by Andrey Vul, Oct 27, 2009.

  1. Andrey Vul

    Andrey Vul Guest

    Is use of strchr(const char *s, '\0') undefined as as a short form of
    (s + strlen(s)) ?
    The problem is that memchr requires strlen, so strchr is the only way
    to do uncounted char find. Is there a way to use it so that it returns
    the address of the '\0' in s?
     
    Andrey Vul, Oct 27, 2009
    #1
    1. Advertising

  2. Andrey Vul

    Andrey Vul Guest

    On Oct 27, 3:39 pm, Andrey Vul <> wrote:
    > Is use of strchr(const char *s, '\0') undefined as as a short form of
    > (s + strlen(s)) ?
    > The problem is that memchr requires strlen, so strchr is the only way
    > to do uncounted char find. Is there a way to use it so that it returns
    > the address of the '\0' in s?


    The point of the question is for an implementation of strpcat()
    (similar to mempcpy() ), i.e. to allow strcat() chaining (strcat(strcat
    (...),...).
     
    Andrey Vul, Oct 27, 2009
    #2
    1. Advertising

  3. Andrey Vul

    Andrey Vul Guest

    On Oct 27, 3:50 pm, Richard Heathfield <> wrote:
    > In <>,
    >
    > Andrey Vul wrote:
    > > Is use of strchr(const char *s, '\0') undefined as as a short form
    > > of (s + strlen(s)) ?

    >
    > No, that's fine. The Standard specifically provides for this
    > possibility:
    >
    > "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."
    >


    Oh, strchr returns NULL when the sentinel zero of the string has been
    reached _and_ second argument is not zero?
     
    Andrey Vul, Oct 27, 2009
    #3
  4. Andrey Vul

    Andrey Vul Guest

    On Oct 28, 9:56 am, Mr John FO Evans <> wrote:
    > In article
    > <>, AndreyVul <> wrote:
    >
    > om>> The problem is that memchr requires strlen, so strchr is the only way
    > om>> to do uncounted char find. Is there a way to use it so that it returns
    > om>> the address of the '\0' in s?
    >
    > Surely strlen(s)+1 will find the \0


    That returns the index, not the pointer.
    strchr(s, '\0') returns the pointer
     
    Andrey Vul, Oct 28, 2009
    #4
  5. Andrey Vul <> writes:
    > On Oct 28, 9:56 am, Mr John FO Evans <> wrote:
    >> In article
    >> <>, AndreyVul <> wrote:
    >>
    >> om>> The problem is that memchr requires strlen, so strchr is the only way
    >> om>> to do uncounted char find. Is there a way to use it so that it returns
    >> om>> the address of the '\0' in s?
    >>
    >> Surely strlen(s)+1 will find the \0

    >
    > That returns the index, not the pointer.
    > strchr(s, '\0') returns the pointer


    So does s + strlen(s).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 28, 2009
    #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,154
    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,278
    Irrwahn Grausewitz
    Aug 16, 2003
  3. Servé Lau

    a shorter strchr

    Servé Lau, Feb 17, 2004, in forum: C Programming
    Replies:
    9
    Views:
    692
  4. David Warner

    strchr & finding multiple occurances of a char

    David Warner, Apr 25, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,398
    CBFalconer
    Apr 26, 2005
  5. ts

    strchr declaration

    ts, Mar 14, 2007, in forum: C Programming
    Replies:
    2
    Views:
    414
    Florian Weingarten
    Mar 14, 2007
Loading...

Share This Page