Portability of strstr() function

O

OzBob

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
 
R

Richard Bos

OzBob said:
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
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
 
E

Eric Sosman

OzBob said:
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')
 
S

Stan Milam

OzBob said:
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.
 
F

Flash Gordon

OzBob said:
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 said:
/* 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>
 
K

Keith Thompson

OzBob said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top