By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Portability of strstr() function

P: n/a
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
Dec 27 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"OzBob" <i.********@btinternet.com> 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
Dec 27 '05 #2

P: n/a
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
es*****@acm-dot-org.invalid
Dec 27 '05 #3

P: n/a
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.
Dec 27 '05 #4

P: n/a
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.
Dec 27 '05 #5

P: n/a
"OzBob" <i.********@btinternet.com> 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) ks***@mib.org <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.
Dec 27 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.