What is valid std::strstr()'s signature?

Discussion in 'C++' started by Alex Vinokur, Oct 17, 2011.

  1. Alex Vinokur

    Alex Vinokur Guest

    Hi,

    What is valid std::strstr()'s signature?


    http://www.cplusplus.com/reference/clibrary/cstring/strstr/
    const char * strstr ( const char * str1, const char * str2 );
    char * strstr ( char * str1, const char * str2 );



    HP-UX B.11.23 U ia64
    /usr/include/string.h: extern char *strstr(const char *, const
    char *);


    Linux 2.6.18-238.1.1.el5
    /usr/include/string.h:extern char *strstr (__const char *__haystack,
    __const char *__needle)


    Thanks,

    Alex
    Alex Vinokur, Oct 17, 2011
    #1
    1. Advertising

  2. Alex Vinokur

    Nobody Guest

    On Mon, 17 Oct 2011 03:50:59 -0700, Alex Vinokur wrote:

    > What is valid std::strstr()'s signature?


    This (21.7p7):

    > const char * strstr ( const char * str1, const char * str2 );
    > char * strstr ( char * str1, const char * str2 );


    > HP-UX B.11.23 U ia64
    > /usr/include/string.h:


    This is the ISO C definition:

    > extern char *strstr(const char *, const char *);


    C doesn't have overloaded functions, so there can only be a single
    prototype. The strstr() function doesn't modify either string (hence the
    "const" on the argument types), but the return value (if not NULL) is a
    pointer into the target string specified by the first argument.

    If the target string can safely be modified, then it's safe to modify it
    via the returned pointer (hence no "const" on the return type). If the
    target string should not be modified, then it's not safe to modify it via
    the returned pointer either. The C++ definition uses overloading to encode
    these rules.
    Nobody, Oct 17, 2011
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    On Oct 17, 2:52 pm, Nobody <> wrote:
    > On Mon, 17 Oct 2011 03:50:59 -0700, Alex Vinokur wrote:
    > > What is valid std::strstr()'s signature?

    >
    > This (21.7p7):
    >
    > > const char * strstr ( const char * str1, const char * str2 );
    > >       char * strstr (       char * str1, const char * str2 );
    > > HP-UX B.11.23 U ia64
    > > /usr/include/string.h:

    >
    > This is the ISO C definition:
    >
    > > extern char *strstr(const char *, const char *);

    >
    > C doesn't have overloaded functions, so there can only be a single
    > prototype.  The strstr() function doesn't modify either string (hence the
    > "const" on the argument types), but the return value (if not NULL) is a
    > pointer into the target string specified by the first argument.
    >
    > If the target string can safely be modified, then it's safe to modify it
    > via the returned pointer (hence no "const" on the return type). If the
    > target string should not be modified, then it's not safe to modify it via
    > the returned pointer either. The C++ definition uses overloading to encode
    > these rules.


    Thanks.
    But where are C++-strstr()'s declared?
    string.h contains the only strstr() declaration.

    Alex
    Alex Vinokur, Oct 17, 2011
    #3
  4. On 10/17/2011 9:20 AM, Alex Vinokur wrote:
    > On Oct 17, 2:52 pm, Nobody<> wrote:
    >> On Mon, 17 Oct 2011 03:50:59 -0700, Alex Vinokur wrote:
    >>> What is valid std::strstr()'s signature?

    >>
    >> This (21.7p7):
    >>
    >>> const char * strstr ( const char * str1, const char * str2 );
    >>> char * strstr ( char * str1, const char * str2 );
    >>> HP-UX B.11.23 U ia64
    >>> /usr/include/string.h:

    >>
    >> This is the ISO C definition:
    >>
    >>> extern char *strstr(const char *, const char *);

    >>
    >> C doesn't have overloaded functions, so there can only be a single
    >> prototype. The strstr() function doesn't modify either string (hence the
    >> "const" on the argument types), but the return value (if not NULL) is a
    >> pointer into the target string specified by the first argument.
    >>
    >> If the target string can safely be modified, then it's safe to modify it
    >> via the returned pointer (hence no "const" on the return type). If the
    >> target string should not be modified, then it's not safe to modify it via
    >> the returned pointer either. The C++ definition uses overloading to encode
    >> these rules.

    >
    > Thanks.
    > But where are C++-strstr()'s declared?
    > string.h contains the only strstr() declaration.


    In <cstdlib>. See Standard, [lib.c.strings]/10.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 17, 2011
    #4
  5. Alex Vinokur

    Marc Guest

    Alex Vinokur wrote:

    > What is valid std::strstr()'s signature?
    > const char * strstr ( const char * str1, const char * str2 );
    > char * strstr ( char * str1, const char * str2 );


    The two above are the correct ones.

    > Linux 2.6.18-238.1.1.el5
    > /usr/include/string.h:extern char *strstr (__const char *__haystack,
    > __const char *__needle)


    That's ancient, recent glibc has the correct declarations (and solaris
    has had them for a very long time).
    Marc, Oct 17, 2011
    #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. JJBW
    Replies:
    1
    Views:
    10,039
    Joerg Jooss
    Apr 24, 2004
  2. Sean Bartholomew

    parsing using strstr

    Sean Bartholomew, Jul 9, 2004, in forum: C++
    Replies:
    3
    Views:
    2,600
    Sean Bartholomew
    Jul 11, 2004
  3. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,337
    Howard
    Nov 29, 2004
  4. Joona I Palaste

    Re: memcmp versus strstr; reaction to chr(0)

    Joona I Palaste, Jul 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    418
    Joona I Palaste
    Jul 24, 2003
  5. Gustavo Narea
    Replies:
    14
    Views:
    847
    Gustavo Narea
    Feb 16, 2009
Loading...

Share This Page