473,763 Members | 7,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Portability / compatibility issues

I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each compiler.
The crux of the matter is that the 'strstr' function differs wildly in its
behaviour on different platforms, and precious few man pages will actually
provide a worked example of any function, let alone this one. I have had to
write the "has_slash" function and use the reserved work SLASH already, to
get around the problems that strstr() gives me.

I know C is touted as an standardised development environment that provides
portability (the immortal phrase "we converted to another platform by typing
'make'" springs to mind); I'm not seeing that right now.

Any advise / comments? Share and Enjoy, Ian

#include <stdio.h>

#define SLASH "/"

int has_slash(char in_str[24])
{
int i=0;

while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100) i++;

/* wrong format */
if (in_str[i] != *SLASH)
{
i=0;
} else
{ i++;
}
printf("In function - i = %i\n", i);
return(i);

}

void main()
{
int test_true;

/* PROVEN ON BOTH PA_RISC AND SPARC */
test_true=has_s lash("copy /f file");
if (test_true != 0) printf("Has slash = true\n");

/* New test, strstr results */
char *str_result;
char test_date[24];

strcpy(test_dat e, "02_04_2005 ");
if (strstr(test_da te, SLASH) != NULL) printf(" By self- found
slash\n");
if (strstr(test_da te, SLASH) == NULL) printf(" By self- not found
slash\
n");

str_result=strs tr(test_date, "/");

if (str_result != NULL) printf("Found slash :%s:!\n", str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n",
str_result);
}
/* server1: PA_RISC with c99 compatible compiler */
/* server1:/tmp> ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Not found slash ::!
server1:/tmp> */

/* server 2: SPARC with gcc */
/* # ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#
/
Jan 15 '06 #1
23 1940
OzBob wrote:
I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each compiler.
The crux of the matter is that the 'strstr' function differs wildly in its
behaviour on different platforms, and precious few man pages will actually
provide a worked example of any function, let alone this one. I have had to
write the "has_slash" function and use the reserved work SLASH already, to
get around the problems that strstr() gives me.

I know C is touted as an standardised development environment that provides
portability (the immortal phrase "we converted to another platform by typing
'make'" springs to mind); I'm not seeing that right now.

Any advise / comments? Share and Enjoy, Ian

Yes: Compile with the highest warning level and in a standard C
mode. E.g.

$ gcc -std=c99 -pedantic -W -Wall -O strstr.c -c
strstr.c: In function `has_slash':
strstr.c:9: warning: comparison between pointer and integer
strstr.c: At top level:
strstr.c:24: warning: return type of 'main' is not `int'
strstr.c: In function `main':
strstr.c:35: warning: implicit declaration of function `strcpy'
strstr.c:36: warning: implicit declaration of function `strstr'
strstr.c:36: error: missing terminating " character
strstr.c:37: error: stray '\' in program
strstr.c:37: error: `slash' undeclared (first use in this function)
strstr.c:37: error: (Each undeclared identifier is reported only once
strstr.c:37: error: for each function it appears in.)
strstr.c:37: error: parse error before "n"
strstr.c:36: warning: empty body in an if-statement
strstr.c:37: error: missing terminating " character
strstr.c:38: error: `n' undeclared (first use in this function)
strstr.c:38: error: missing terminating " character
strstr.c:39: error: missing terminating " character

where the include directive is on line 1.

Your program does not compile -- so how can you expect us to
look at it?

Cheers
Michael
#include <stdio.h>

#define SLASH "/"

int has_slash(char in_str[24])
{
int i=0;

while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100) i++;

/* wrong format */
if (in_str[i] != *SLASH)
{
i=0;
} else
{ i++;
}
printf("In function - i = %i\n", i);
return(i);

}

void main()
{
int test_true;

/* PROVEN ON BOTH PA_RISC AND SPARC */
test_true=has_s lash("copy /f file");
if (test_true != 0) printf("Has slash = true\n");

/* New test, strstr results */
char *str_result;
char test_date[24];

strcpy(test_dat e, "02_04_2005 ");
if (strstr(test_da te, SLASH) != NULL) printf(" By self- found
slash\n");
if (strstr(test_da te, SLASH) == NULL) printf(" By self- not found
slash\
n");

str_result=strs tr(test_date, "/");

if (str_result != NULL) printf("Found slash :%s:!\n", str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n",
str_result);
}
/* server1: PA_RISC with c99 compatible compiler */
/* server1:/tmp> ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Not found slash ::!
server1:/tmp> */

/* server 2: SPARC with gcc */
/* # ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#
/

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jan 15 '06 #2

"Michael Mair" <Mi**********@i nvalid.invalid> wrote in message
news:42******** *****@individua l.net...
OzBob wrote:
I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each compiler.
The crux of the matter is that the 'strstr' function differs wildly in
its behaviour on different platforms, and precious few man pages will
actually provide a worked example of any function, let alone this one. I
have had to write the "has_slash" function and use the reserved work
SLASH already, to get around the problems that strstr() gives me.

I know C is touted as an standardised development environment that
provides portability (the immortal phrase "we converted to another
platform by typing 'make'" springs to mind); I'm not seeing that right
now.

Any advise / comments? Share and Enjoy, Ian

Yes: Compile with the highest warning level and in a standard C
mode. E.g.

$ gcc -std=c99 -pedantic -W -Wall -O strstr.c -c
strstr.c: In function `has_slash':
strstr.c:9: warning: comparison between pointer and integer
strstr.c: At top level:
strstr.c:24: warning: return type of 'main' is not `int'
strstr.c: In function `main':
strstr.c:35: warning: implicit declaration of function `strcpy'
strstr.c:36: warning: implicit declaration of function `strstr'
strstr.c:36: error: missing terminating " character
strstr.c:37: error: stray '\' in program
strstr.c:37: error: `slash' undeclared (first use in this function)
strstr.c:37: error: (Each undeclared identifier is reported only once
strstr.c:37: error: for each function it appears in.)
strstr.c:37: error: parse error before "n"
strstr.c:36: warning: empty body in an if-statement
strstr.c:37: error: missing terminating " character
strstr.c:38: error: `n' undeclared (first use in this function)
strstr.c:38: error: missing terminating " character
strstr.c:39: error: missing terminating " character

where the include directive is on line 1.

Your program does not compile -- so how can you expect us to
look at it?

Cheers
Michael

#include <stdio.h>

#define SLASH "/"

int has_slash(char in_str[24])
{
int i=0;

while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100) i++;

/* wrong format */
if (in_str[i] != *SLASH)
{
i=0;
} else
{ i++;
}
printf("In function - i = %i\n", i);
return(i);

}

void main()
{
int test_true;

/* PROVEN ON BOTH PA_RISC AND SPARC */
test_true=has_s lash("copy /f file");
if (test_true != 0) printf("Has slash = true\n");

/* New test, strstr results */
char *str_result;
char test_date[24];

strcpy(test_dat e, "02_04_2005 ");
if (strstr(test_da te, SLASH) != NULL) printf(" By self- found
slash\n");
if (strstr(test_da te, SLASH) == NULL) printf(" By self- not found
slash\
n");

str_result=strs tr(test_date, "/");

if (str_result != NULL) printf("Found slash :%s:!\n",
str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n",
str_result);
}
/* server1: PA_RISC with c99 compatible compiler */
/* server1:/tmp> ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Not found slash ::!
server1:/tmp> */

/* server 2: SPARC with gcc */
/* # ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#
/

--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Micheal,

Have submitted the commands for gcc, with the following response. Have also
checked the code and string.h is not included. I have since modified the
comments at the end, included the <string.h> library at the start, and
succesfully compiled using the commands provided. I even went a step further
and removed the "-c" option from the command string to generate "a.out",
which (in the interests of consistency) fails at the same point with the
same error.

I figure its the strstr command and the assignment of a pointer, which
again, works fine on the HP-UX 'cc' compiler (the full one, not the inbuilt
kernel-only one) with the '-AC99' c99 compatibility option, yet bails on the
gcc compiler.

Any further ideas? Share and Enjoy, OzBob

/* original compiler output */
# gcc -std=c99 -pedantic -W -Wall -O test.c -c
test.c:24: warning: return type of `main' is not `int'
test.c: In function `main':
test.c:35: warning: implicit declaration of function `strcpy'
test.c:36: warning: implicit declaration of function `strstr'
test.c:53:1: unterminated comment

/* included <string.h>, terminated comment */
# gcc -std=c99 -pedantic -W -Wall -O test.c
test.c:25: warning: return type of `main' is not `int'
ls # -l a.out
-rwxr-xr-x 1 root other 7280 Jan 15 15:04 a.out
# ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#

/* Context information */
# gcc --version
gcc (GCC) 3.3.2
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# uname -a
SunOS sunhome1 5.9 Generic_112233-07 sun4m sparc SUNW,SPARCstati on-4
#
Jan 15 '06 #3
Ico
Michael Mair <Mi**********@i nvalid.invalid> wrote:
OzBob wrote:
I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each compiler.
The crux of the matter is that the 'strstr' function differs wildly in its
behaviour on different platforms, and precious few man pages will actually
provide a worked example of any function, let alone this one. I have had to
write the "has_slash" function and use the reserved work SLASH already, to
get around the problems that strstr() gives me.

I know C is touted as an standardised development environment that provides
portability (the immortal phrase "we converted to another platform by typing
'make'" springs to mind); I'm not seeing that right now.

Any advise / comments? Share and Enjoy, Ian

Yes: Compile with the highest warning level and in a standard C
mode. E.g.

$ gcc -std=c99 -pedantic -W -Wall -O strstr.c -c


<snipped a lot of warnings and errors>
Your program does not compile -- so how can you expect us to
look at it?


It *does* compile, if you only fix the line wraps, probably caused by
the OT's newsreader software. These things do happen.

--
:wq
^X^Cy^K^X^C^C^C ^C
Jan 15 '06 #4

"OzBob" <i.********@bti nternet.com> wrote in message
news:dq******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...

"Michael Mair" <Mi**********@i nvalid.invalid> wrote in message
news:42******** *****@individua l.net...
OzBob wrote:
I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment
(PA-RISC running HP-UX 11.11 and c99 compatible compiler). However I
seem to have encountered problems performing the most basic of
manipulations and comparisons.

I have posted my test code below, and the results I get on each
compiler. The crux of the matter is that the 'strstr' function differs
wildly in its behaviour on different platforms, and precious few man
pages will actually provide a worked example of any function, let alone
this one. I have had to write the "has_slash" function and use the
reserved work SLASH already, to get around the problems that strstr()
gives me.

I know C is touted as an standardised development environment that
provides portability (the immortal phrase "we converted to another
platform by typing 'make'" springs to mind); I'm not seeing that right
now.

Any advise / comments? Share and Enjoy, Ian

Yes: Compile with the highest warning level and in a standard C
mode. E.g.

$ gcc -std=c99 -pedantic -W -Wall -O strstr.c -c
strstr.c: In function `has_slash':
strstr.c:9: warning: comparison between pointer and integer
strstr.c: At top level:
strstr.c:24: warning: return type of 'main' is not `int'
strstr.c: In function `main':
strstr.c:35: warning: implicit declaration of function `strcpy'
strstr.c:36: warning: implicit declaration of function `strstr'
strstr.c:36: error: missing terminating " character
strstr.c:37: error: stray '\' in program
strstr.c:37: error: `slash' undeclared (first use in this function)
strstr.c:37: error: (Each undeclared identifier is reported only once
strstr.c:37: error: for each function it appears in.)
strstr.c:37: error: parse error before "n"
strstr.c:36: warning: empty body in an if-statement
strstr.c:37: error: missing terminating " character
strstr.c:38: error: `n' undeclared (first use in this function)
strstr.c:38: error: missing terminating " character
strstr.c:39: error: missing terminating " character

where the include directive is on line 1.

Your program does not compile -- so how can you expect us to
look at it?

Cheers
Michael

#include <stdio.h>

#define SLASH "/"

int has_slash(char in_str[24])
{
int i=0;

while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100) i++;

/* wrong format */
if (in_str[i] != *SLASH)
{
i=0;
} else
{ i++;
}
printf("In function - i = %i\n", i);
return(i);

}

void main()
{
int test_true;

/* PROVEN ON BOTH PA_RISC AND SPARC */
test_true=has_s lash("copy /f file");
if (test_true != 0) printf("Has slash = true\n");

/* New test, strstr results */
char *str_result;
char test_date[24];

strcpy(test_dat e, "02_04_2005 ");
if (strstr(test_da te, SLASH) != NULL) printf(" By self- found
slash\n");
if (strstr(test_da te, SLASH) == NULL) printf(" By self- not
found slash\
n");

str_result=strs tr(test_date, "/");

if (str_result != NULL) printf("Found slash :%s:!\n",
str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n",
str_result);
}
/* server1: PA_RISC with c99 compatible compiler */
/* server1:/tmp> ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Not found slash ::!
server1:/tmp> */

/* server 2: SPARC with gcc */
/* # ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#
/

--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Micheal,

Have submitted the commands for gcc, with the following response. Have
also checked the code and string.h is not included. I have since modified
the comments at the end, included the <string.h> library at the start, and
succesfully compiled using the commands provided. I even went a step
further and removed the "-c" option from the command string to generate
"a.out", which (in the interests of consistency) fails at the same point
with the same error.

I figure its the strstr command and the assignment of a pointer, which
again, works fine on the HP-UX 'cc' compiler (the full one, not the
inbuilt kernel-only one) with the '-AC99' c99 compatibility option, yet
bails on the gcc compiler.

Any further ideas? Share and Enjoy, OzBob

/* original compiler output */
# gcc -std=c99 -pedantic -W -Wall -O test.c -c
test.c:24: warning: return type of `main' is not `int'
test.c: In function `main':
test.c:35: warning: implicit declaration of function `strcpy'
test.c:36: warning: implicit declaration of function `strstr'
test.c:53:1: unterminated comment

/* included <string.h>, terminated comment */
# gcc -std=c99 -pedantic -W -Wall -O test.c
test.c:25: warning: return type of `main' is not `int'
ls # -l a.out
-rwxr-xr-x 1 root other 7280 Jan 15 15:04 a.out
# ./a.out
In function - i = 6
Has slash = true
By self- not found slash
Segmentation Fault - core dumped
#

/* Context information */
# gcc --version
gcc (GCC) 3.3.2
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

# uname -a
SunOS sunhome1 5.9 Generic_112233-07 sun4m sparc SUNW,SPARCstati on-4
#


while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100) i++; while (in_str[i] != *SLASH && in_str[i] != '\0' && i < 100) i++;
OR
while (in_str[i] != *SLASH && in_str[i] && i < 100) i++;
OR, if it's always a nul terminated string
while (in_str[i] != *SLASH && in_str[i] i++;


OR

Perhaps a rewrite of some sort? ...

int has_slash(char in_str[24])
{
for(int i = 0; in_str[i] != '\0'; ++i)
{
if(in_str[i] == *SLASH)
{
return i;
}
}

return 0;
}

Jan 15 '06 #5
Ico
OzBob <i.********@bti nternet.com> wrote:

"Michael Mair" <Mi**********@i nvalid.invalid> wrote in message
news:42******** *****@individua l.net...
OzBob wrote:
I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each compiler.
The crux of the matter is that the 'strstr' function differs wildly in
its behaviour on different platforms, and precious few man pages will
actually provide a worked example of any function, let alone this one.
That's very strange. strstr() is very well standarized, and should not
give you any troubles.
I have had to write the "has_slash" function and use the reserved
work SLASH already, to get around the problems that strstr() gives
me.
Maybe your 'problem' is that you don't expect strstr() to return NULL
when the string you are searching for is not found ?
I know C is touted as an standardised development environment that
provides portability (the immortal phrase "we converted to another
platform by typing 'make'" springs to mind); I'm not seeing that right
now.
That's very well possible, considering your code is written with
portability in mind.
Any advise / comments? Share and Enjoy, Ian
Some comments :

- include string.h (you already did)
- main() is supposed to return an int. The proper definition is
int main(void) or int main(int argc, char *argv[])
str_result=strs tr(test_date, "/");

if (str_result != NULL) printf("Found slash :%s:!\n", str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n", str_result); [...] By self- not found slash
Segmentation Fault - core dumped


That's no so strange : strstr() returns NULL when "/" is not found in
"02_04_2005 ". Thus, str_result is NULL, but you pass it to printf(),
which does not like NULL arguments.

The obvious solution would be to check the return value of strstr() for
NULL.

Regards,

Ico

--
:wq
^X^Cy^K^X^C^C^C ^C
Jan 15 '06 #6

"Ico" <us****@zevv.nl > wrote in message
news:43******** **************@ dreader32.news. xs4all.nl...
OzBob <i.********@bti nternet.com> wrote:

"Michael Mair" <Mi**********@i nvalid.invalid> wrote in message
news:42******** *****@individua l.net...
OzBob wrote:
I am developing some basic string / file manipulation C progams at home
(Sparcstation running Solaris 9 and gcc) for my work environment
(PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to
have
encountered problems performing the most basic of manipulations and
comparisons.

I have posted my test code below, and the results I get on each
compiler.
The crux of the matter is that the 'strstr' function differs wildly in
its behaviour on different platforms, and precious few man pages will
actually provide a worked example of any function, let alone this one.
That's very strange. strstr() is very well standarized, and should not
give you any troubles.
I have had to write the "has_slash" function and use the reserved
work SLASH already, to get around the problems that strstr() gives
me.
Maybe your 'problem' is that you don't expect strstr() to return NULL
when the string you are searching for is not found ?
I know C is touted as an standardised development environment that
provides portability (the immortal phrase "we converted to another
platform by typing 'make'" springs to mind); I'm not seeing that right
now.
That's very well possible, considering your code is written with
portability in mind.
Any advise / comments? Share and Enjoy, Ian
Some comments :

- include string.h (you already did)
- main() is supposed to return an int. The proper definition is
int main(void) or int main(int argc, char *argv[])
str_result=strs tr(test_date, "/");

if (str_result != NULL) printf("Found slash :%s:!\n", str_result);
if (str_result == NULL) printf("Not found slash :%s:!\n", str_result); [...] By self- not found slash
Segmentation Fault - core dumped


That's no so strange : strstr() returns NULL when "/" is not found in
"02_04_2005 ". Thus, str_result is NULL, but you pass it to printf(),
which does not like NULL arguments.

The obvious solution would be to check the return value of strstr() for
NULL.

Regards,

Ico

--
:wq
^X^Cy^K^X^C^C^C ^C


Ico,

Appreciate the feedback. I think I need to simplify my code to demonstrate
what the precise problem is. I will remove the NULL values from the printf()
command and see what I get. Will reply from work tomorrow

Thanks all for the input so far, Share and Enjoy! OzBob
Jan 15 '06 #7
OzBob wrote:
"Michael Mair" <Mi**********@i nvalid.invalid> wrote:
.... snip ...

Your program does not compile -- so how can you expect us to
look at it?

.... snip ...
Have submitted the commands for gcc, with the following
response. Have also checked the code and string.h is not
included. I have since modified the comments at the end,
included the <string.h> library at the start, and succesfully
compiled using the commands provided. I even went a step further
and removed the "-c" option from the command string to generate
"a.out", which (in the interests of consistency) fails at the
same point with the same error.

I figure its the strstr command and the assignment of a pointer,
which again, works fine on the HP-UX 'cc' compiler (the full
one, not the inbuilt kernel-only one) with the '-AC99' c99
compatibility option, yet bails on the gcc compiler. Any further
ideas? Share and Enjoy, OzBob

/* original compiler output */
# gcc -std=c99 -pedantic -W -Wall -O test.c -c
test.c:24: warning: return type of `main' is not `int'
test.c: In function `main':
test.c:35: warning: implicit declaration of function `strcpy'
test.c:36: warning: implicit declaration of function `strstr'
test.c:53:1: unterminated comment


Obviously you haven't fixed the code. The above is not what we
consider 'compiling'. The warnings and the error are serious.

Keep code line length under 72 chars for Usenet posting. 65 is
even better. This will avoid awkward wraps.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Jan 15 '06 #8
OzBob a écrit :
Any advise / comments? Share and Enjoy, Ian


#include <stdio.h>

/* -ed- was missing (strcpy()) */
#include <string.h>

#define SLASH "/"

/* -ed- not exported function. static added.
Accepts string literals : const added (see further)
static int has_slash(char const in_str[24])

a complicated way of writing
*/
static int has_slash (char const *in_str)
{
int i = 0;

/* -ed-
while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100)

you don't want 'NULL' but 0

{} added for readability and maintenance.

What the hell is this 100 ?
*/
while (in_str[i] != *SLASH && in_str[i] != 0)
{
i++;
}
/* wrong format */
if (in_str[i] != *SLASH)
{
i = 0;
}
else
{
i++;
}
printf("In function - i = %i\n", i);

/* -ed-
return (i);

useless parens
*/
return i;

}

/* -ed-
void main()

main() returns int. Always.
*/
int main (void)
{
int test_true;

/* PROVEN ON BOTH PA_RISC AND SPARC */

/* -ed- warning : a string litteral is not mutable.
char const * expected. */
test_true = has_slash("copy /f file");

if (test_true != 0)
{
printf("Has slash = true\n");
}

/* -ed- C90 wants data definitions to be at the top of a block. */
{
/* New test, strstr results */
char *str_result;
char test_date[24];

strcpy(test_dat e, "02_04_2005 ");
if (strstr(test_da te, SLASH) != NULL)
/* -ed-

printf(" By self- found
slash\n");
uncomplete string. */
{
printf(" By self- found slash\n");
}

/* -ed- sounds that we have a job for if-else too... */
if (strstr(test_da te, SLASH) == NULL)
/* -ed-
printf(" By self- not found
slash\
n");

what the heck are you doing there ?
*/
{
printf(" By self- not found slash\n");
}

str_result = strstr(test_dat e, "/");

if (str_result != NULL)
{
printf("Found slash :%s:!\n", str_result);
}

/* -ed- sounds that we have a job for if-else too... */
if (str_result == NULL)
{
/* -ed- do not ettempt to printout a NULL pointer.
The behaviour is undefined. */
printf("Not found slash :%s:!\n",
str_result);
}
}
/* -ed- because main() returnes int... */
return 0;
}
--
A+

Emmanuel Delahaye
Jan 15 '06 #9
In article <dq**********@n wrdmz03.dmz.ncs .ea.ibs-infra.bt.com>
OzBob <i.********@bti nternet.com> wrote:
I am developing some basic string / file manipulation C progams at home
(Sparcstatio n running Solaris 9 and gcc) for my work environment (PA-RISC
running HP-UX 11.11 and c99 compatible compiler). However I seem to have
encountered problems performing the most basic of manipulations and
comparisons.
Actually, your immediate problem is with the line:

printf("some text including the %s directive\n", (char *)NULL);

which is not a valid operation: the effect of providing NULL to
the "%s" directive is undefined. On the HP-UX machine, the actual
effect turns out to be a no-op, and on the SPARC, it turns out to
be "Segmentati on Fault - core dumped".

Besdies the comments others have posted, the has_slash() function
is coded quite oddly:
int has_slash(char in_str[24])
{
It is worth noting that the actual type of "in_str" within has_slash
is "char *", not "char [24]". The size provided in the brackets is
ignored and the declaration runs into the following sentence (from
the C99 draft, section 6.7.1):

A declaration of a parameter as ``array of type'' shall be
adjusted to ``pointer to type,'' and a declaration of a
parameter as ``function returning type'' shall be adjusted to
``pointer to function returning type,'' as in 6.2.2.1.

Thus, you could equally write:

int has_slash(char *in_str) {

here, omitting the constant 24. But since you did include it:
int i=0;

while (in_str[i] != *SLASH && in_str[i] != NULL && i < 100) i++;


.... it seems quite odd that you would limit "i" to below-100 instead
of below-24.

In any case, if NULL is defined as ((void *)0) (which is allowed,
but apparently not the case on either of the two ocmpilers you have
used so far), the comparison:

in_str[i] != NULL

will "draw a diagnostic" (produce a warning or error message). The
correct test is:

in_str[i] != '\0'

or similar: '\0' is an int with value zero, so you can write any
int with value zero here, including 0, 0x0, (34 + 43 - 77), etc.
I prefer to use '\0' to emphasize the "character-ness" of the test:
we are looking to stop when we reach the NUL character (the one-L
NUL; not the two-L NULL pointer[%]).

[
The one-l lama
He's a priest
The two-l llama
He's a beast
And I will bet
A silk pajama
There isn't any
Three-l lllama.*

*The author's attention has been called to a type of
conflagration known as a three-alarmer. Pooh.

Ogden Nash <http://www.who2.com/ogdennash.html> .]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jan 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2180
by: Tim Tyler | last post by:
I just ported one of the apps I wrote using PHP 5 under Windows to PHP 4 under unix. I knew I wanted to deploy in this environment - and wrote my code in anticipation of running in the other environment. A brief summary follows: Portability was not too bad. However there were several problems I did not anticipate.
7
1621
by: fabio de francesco | last post by:
Hi, I'm not a professional programmer, but I've been writing C/C++ and Ada programs for a few years on GNU/Linux without ever concerning on standards and portability to other OSs. I've always noted that when I write code I must use lots of platform specific system calls (POSIX and X/OPEN). Often I found myself using threads and concurrent processes with some sort of IPC. Some time I need some socket API. When I just want to open a...
21
1928
by: asm | last post by:
Hi All, Like typdef, does C have further support for portability? Thanks, ASM
93
3672
by: roman ziak | last post by:
I just read couple articles on this group and it keeps amazing me how the portability is used as strong argument for language cleanliness. In my opinion, porting the program (so you just take the source code and recompile) is a myth started 20-30 years ago when world consisted of UNIX systems. Well, world does not consist of UNIX systems anymore, but there are 100s of different systems running in cell-phones, DVD players, game consoles...
2
3042
by: G2 | last post by:
Hi We are dealing with significant browser compatibility issues with Netscape 5.x+ browsers and Mac IE. I am sure most web developers have faced similar issues in the past. Can anyone give me their thoughts on how they were able to address these issues ? Are there any best practices published by MS on how to ensure browser compatibility when coding asp.net Thanks for any input.
2
1533
by: Calvin KD | last post by:
Does anyone know if there's any issues involving versions of .Net Framework, Visual Studio and HTTP which might cause a compatibility problem? We currently using VS1.1 for our web apps (C#) and the app seems to be up and and down quite regularly on production and we're not quite sure why. Our techies told us that because we're (developers) using the wrong version of http, 1 instead of 1.1, which cause some sort of compatibility issues...
239
10280
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my users request are a part of the OpenSource community. Many if not most of those applications strongly require the presence of the GNU compiling suite to work properly. My assumption is that this is due to the author/s creating the applications...
10
3821
by: Lionel B | last post by:
Greetings, I have some code that is to read unformatted data from disc and interpret it as blocks of unsigned integers. In an attempt to achieve efficiency (it is pretty essential for my application that the code be speed optimized ) I use reinterpret_cast to alias a block of chars read in from disc as a block of integer "words". My existing code (see simplified code below) appears to work well enough on the platforms available to me,...
18
409
by: jacob navia | last post by:
One of the holy cows here is this "portability" stuff. In practice, portability means: 1) Use the least common denominator of all the supported systems. 2) Between usability / good user experience and portability always choose portability since this minimizes programming effort
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.