some unusual errors

J. J. Farrell jjf@bcs.org.uk
Wed Sep 23 19:20:00 GMT 1998


> From: Michael Richardson <mcr@solidum.com>
> 
> 
> >>>>> "Peter" == Peter Dalgaard BSA <p.dalgaard@biostat.ku.dk> writes:
> 
>     Peter> Michael Richardson <mcr@solidum.com> writes:
> 
>     >> >> strtod.c:1239: warning: subscript has type `char' >> which confuses
>     >> me. What, if not "char" should isspace() take???
>     >> 
>     Michael> As all is<xxx> functions it takes an *int* or *unsigned char* !
>     >>
>     Michael> int isalnum (int c); int isalpha (int c);
>     >>  Why would you say "unsigned char"??  It says "int" to me.  So, a
>     >> signed char should promote to int just fine.
> 
>     Peter> ...with everything above 127 mapping to negative numbers!
> 
>   Yes. That's my problem. 
>   The macro is wrong: it should behave in the same way as a prototyped
> function.

I agree entirely with what you say, but I think it may not be what
you mean! The only way in which the function version will behave
differently from the macro version is that the function version
won't give you the spurious warning message. If you pass a signed
char which contains a negative value (top bit set) other than EOF
to any of the ctype macros or functions, you will get undefined
behaviour - it may do what you want by luck, or it may crash your
program or do anything else it fancies.

If you can be certain that the values in your signed chars are
between 0 and UCHAR_MAX, there's no problem passing those signed
chars to a ctype function - but you have to put up with the spurious
warning in this implementation. If you can't be certain of that, you
must do something to make certain - either check their value before 
calling is*(), or access them as unsigned if that's appropriate.
The most portable fix to your example bit of code would be

      for(s = s00; isspace(*(unsigned char*)s); s++)

or just declare s as <unsigned char *> in the first place.

[ Casting the char itself to unsigned is not a portable answer to
  this since it will change the bit pattern on a 1's complement
  machine; probably not what is wanted. ]

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".



More information about the Cygwin mailing list