Portability of strstr() function

Discussion in 'C Programming' started by OzBob, Dec 27, 2005.

  1. OzBob

    OzBob Guest

    Am performing the following check to determine if a set of text is at the
    start of a string,....

    /* Check for literal text DD at beginning of string date_format */
    char date_format[24];
    if (strstr(date_format, "DD") != date_format)
    {
    /* perform swap here */
    }

    Is there something wrong with the structure here? I know that strstr()
    returns a pointer, and "date_format" is a pointer to the first character of
    the string.

    I perform this on Solaris 9 using gcc, and it works. I relocate it to HP-UX
    11.11 using the generic 'cc' and it gives me compiler warnings.

    Is there a good guide for portability out there across platforms and
    compilers? Share and Enjoy, Ian Dennison
    OzBob, Dec 27, 2005
    #1
    1. Advertising

  2. OzBob

    Richard Bos Guest

    "OzBob" <> wrote:

    > Am performing the following check to determine if a set of text is at the
    > start of a string,....
    >
    > /* Check for literal text DD at beginning of string date_format */
    > char date_format[24];
    > if (strstr(date_format, "DD") != date_format)
    > {
    > /* perform swap here */
    > }
    >
    > Is there something wrong with the structure here?


    Not as such. The above is guaranteed to work. It's not the most
    efficient way to do it, though, since it'll check the rest of the string
    if it doesn't start with "DD". strncmp() would work just as well, and
    check no more characters than needed.

    > I perform this on Solaris 9 using gcc, and it works. I relocate it to HP-UX
    > 11.11 using the generic 'cc' and it gives me compiler warnings.


    What warnings? Quote please, don't paraphrase. If it complains about
    functions not being declared, you have probably forgotten to #include
    <string.h>.

    > Is there a good guide for portability out there across platforms and
    > compilers? Share and Enjoy, Ian Dennison


    There's the Standard, but I don't know a good downloadable summary of
    the C89 Standard. The latest public draft of C99 is available from the
    'net, but HP-UX cc probably doesn't do C99 yet.

    Richard
    Richard Bos, Dec 27, 2005
    #2
    1. Advertising

  3. OzBob

    Eric Sosman Guest

    OzBob wrote:

    > Am performing the following check to determine if a set of text is at the
    > start of a string,....
    >
    > /* Check for literal text DD at beginning of string date_format */
    > char date_format[24];
    > if (strstr(date_format, "DD") != date_format)
    > {
    > /* perform swap here */
    > }
    >
    > Is there something wrong with the structure here? I know that strstr()
    > returns a pointer, and "date_format" is a pointer to the first character of
    > the string.
    >
    > I perform this on Solaris 9 using gcc, and it works. I relocate it to HP-UX
    > 11.11 using the generic 'cc' and it gives me compiler warnings.
    >
    > Is there a good guide for portability out there across platforms and
    > compilers? Share and Enjoy, Ian Dennison


    strstr() is a Standard library function, so it is present
    in all conforming C implementations. My own, personal hunch:
    you didn't include <string.h> -- but you didn't show enough
    code to support or refute the hunch.

    If you're just checking for "DD" at the start of the string,
    strstr() will do the job but seems to me to be the wrong tool.
    I'd suggest you consider

    if (strncmp(date_format, "DD", 2) != 0)

    or even

    if (date_format[0] != 'D' || date_format[1] != 'D')

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 27, 2005
    #3
  4. OzBob

    Stan Milam Guest

    OzBob wrote:
    > Am performing the following check to determine if a set of text is at the
    > start of a string,....
    >
    > /* Check for literal text DD at beginning of string date_format */
    > char date_format[24];
    > if (strstr(date_format, "DD") != date_format)
    > {
    > /* perform swap here */
    > }
    >
    > Is there something wrong with the structure here? I know that strstr()
    > returns a pointer, and "date_format" is a pointer to the first character of
    > the string.
    >
    > I perform this on Solaris 9 using gcc, and it works. I relocate it to HP-UX
    > 11.11 using the generic 'cc' and it gives me compiler warnings.
    >
    > Is there a good guide for portability out there across platforms and
    > compilers? Share and Enjoy, Ian Dennison
    >
    >


    First, strstr() is very good if you are looking for a pattern match
    *somewhere* in the string. If you know the position strncmp() or
    memcmp() would work much better.

    Secondly, the compiler warnings on HP may be caused because you are
    using the K&R version of the compiler and it does not like some of the
    newer standard syntax. I have found this to be true on HP in the past,
    and currently on AIX. On AIX if I use cc I get a K&R version of the
    compiler. If I use xlc I get a C89 compiler. I was looking the other
    day and found an additional flag the brings xlc into close alignment
    with C99.

    Regards,
    Stan Milam.
    Stan Milam, Dec 27, 2005
    #4
  5. OzBob

    Flash Gordon Guest

    OzBob wrote:
    > Am performing the following check to determine if a set of text is at the
    > start of a string,....


    You should provide a small, complete, compilable example showing your
    problem, not a snippet.

    #include <string.h>

    > /* Check for literal text DD at beginning of string date_format */
    > char date_format[24];


    Some code that loads text in to date_format, I assume, was in here.

    > if (strstr(date_format, "DD") != date_format)
    > {
    > /* perform swap here */
    > }
    >
    > Is there something wrong with the structure here? I know that strstr()
    > returns a pointer, and "date_format" is a pointer to the first character of
    > the string.


    The code you show is correct as far as it goes, but who knows what else
    may be going on that could be the cause of you problem? Not us, as you
    did not provide a complete example.

    > I perform this on Solaris 9 using gcc, and it works. I relocate it to HP-UX
    > 11.11 using the generic 'cc' and it gives me compiler warnings.


    What warning? You should really try to at least give us a clue by
    cutting and pasting the exact text of the warning in to you message.

    > Is there a good guide for portability out there across platforms and
    > compilers? Share and Enjoy, Ian Dennison


    The ISO C standard specifies what all compilers have to provide. Be
    aware that most implementations are the old C89 standard, not the
    current C99 standard. However, if you google for n1124.pdf you can
    download a draft of the latest version (C99 plus a couple of TCs) which
    will at least be a good starting point.

    <OT>
    Depending on the level of portability you want you might also find the
    POSIX standard of use, since although it is not as portable as standard
    C it is portable across POSIX systems including those you list.

    Using gcc if you use "-ansi -pedantic -Wall -O" it should do a
    reasonable job of being a standard compiler and provide a number of
    useful warnings.
    </OT>
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Dec 27, 2005
    #5
  6. "OzBob" <> writes:
    > Am performing the following check to determine if a set of text is at the
    > start of a string,....
    >
    > /* Check for literal text DD at beginning of string date_format */
    > char date_format[24];
    > if (strstr(date_format, "DD") != date_format)
    > {
    > /* perform swap here */
    > }
    >
    > Is there something wrong with the structure here? I know that strstr()
    > returns a pointer, and "date_format" is a pointer to the first character of
    > the string.


    I would expect a warning for the code fragment you posted, since
    date_format isn't initialized. (See the other followups asking you to
    post actual code.)

    --
    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, Dec 27, 2005
    #6
    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. Sean Bartholomew

    parsing using strstr

    Sean Bartholomew, Jul 9, 2004, in forum: C++
    Replies:
    3
    Views:
    2,626
    Sean Bartholomew
    Jul 11, 2004
  2. Joona I Palaste

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

    Joona I Palaste, Jul 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    425
    Joona I Palaste
    Jul 24, 2003
  3. Thomas Matthews

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

    Thomas Matthews, Jul 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    517
    Thomas Matthews
    Jul 24, 2003
  4. Burne C
    Replies:
    3
    Views:
    1,324
    Peter Ammon
    Jul 25, 2003
  5. Dan Pop
    Replies:
    0
    Views:
    375
    Dan Pop
    Jul 24, 2003
Loading...

Share This Page