Re: Portability / compatibility issues



OzBob wrote:
"Michael Mair" <Michael.Mair@xxxxxxxxxxxxxxx> wrote in message news:42v1nlF1l3ge0U1@xxxxxxxxxxxxxxxxx

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?

<snip>
Micheal,

I'm not that Irish ;-)

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'

Note: This is not to be taken lightly -- in principle, all bets are off for a "void main" main().

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
#
<snip>

Look at this one; it is yours with slight changes and works
as I expect:

#include <stdio.h>
#include <string.h>

#define SLASH "/"

int has_slash (const char *in_str);

int main (void)
{
  int     test_true;

  test_true = has_slash("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_date, "02_04_2005");
  if (strstr(test_date, SLASH) != NULL)
    printf(" By self- found slash\n");
  else
    printf(" By self- not found slash\n");

  str_result=strstr(test_date, "/");

  if (str_result != NULL)
    printf("Found slash:     %s[%s]\n",
           str_result, test_date);
  if (str_result == NULL)
    printf("Not found slash: \'\\0\'[%s]\n",
           test_date);

  return 0;
}

int has_slash (const char *in_str)
{
  int     i=0;

  while (in_str[i] != *SLASH && in_str[i] != '\0'
         /* && i < 100 */)
    i++;

  /* wrong format */
  i = (in_str[i] != *SLASH) ? 0 : i+1;

  printf("In function - i = %i\n", i);

  return(i);
}

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



Relevant Pages

  • Re: Portability / compatibility issues
    ... >> I have posted my test code below, and the results I get on each compiler. ... >> SLASH already, to get around the problems that strstr() gives me. ... Compile with the highest warning level and in a standard C ...
    (comp.lang.c)
  • Re: Why INFINITE loop in a thread occupy so much CPU time??
    ... With the attendant warning about the constant expression. ... This is a common programming technique. ... Good programming practice would consist of using the compiler at ... One can write 'bool' in C++ but not C. ...
    (microsoft.public.vc.mfc)
  • Re: help needed please!
    ... But some compilers like to give you a warning ... Each warning a compiler can give you is important when you not ... but you may use flages to handle flags to handle flags when you ... >> stdout is line bufferd and until one or both of the following cases is ...
    (comp.lang.c)
  • Re: Overloading abstract methods
    ... I'm calling this abstract class ResumeWriter. ... > use of in each subclass is up to you. ... >> However, if I do that, I get compiler warnings that the parameters for ... Disable that warning. ...
    (comp.lang.java.programmer)
  • Re: Why does this work?
    ... The compiler is able to tell the difference between the two by the context in which the word "color" is used. ... The point is that the compiler reads the original code as attempting to access a shared member of an instance. ... That's why the compiler is warning the programmer that it's not going to do what was coded. ...
    (microsoft.public.dotnet.languages.vb)