Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Portability of strstr() function

Reply
Thread Tools

Portability of strstr() function

 
 
OzBob
Guest
Posts: n/a
 
      12-27-2005
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


 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      12-27-2005
"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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-27-2005
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
 
Reply With Quote
 
Stan Milam
Guest
Posts: n/a
 
      12-27-2005
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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-27-2005
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-27-2005
"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) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Portability regarding sizeof() function Behzad C Programming 48 04-17-2009 12:02 AM
portability problem with a function returning a struct Army1987 C Programming 19 05-19-2007 10:07 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Portability Francisco Rodriguez VHDL 2 01-23-2004 02:17 PM
code portability and function call serialisation. Lefevre C++ 1 11-12-2003 04:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57