Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Casting ctype lookups



On Wed, Nov 14, 2012 at 10:36:42AM -0500, D'Arcy J.M. Cain wrote:
> 
> Functions yes but the macros don't work the same.
> 
> $ cat t.c
> #include <stdio.h>
> #include <ctype.h>
> void func(char *s)
> {
>   while (*s++) *s = toupper(*s);
> }
> int main(void)
> {
>   char s[] = "Test string";
> 
>   func(s);
>   printf("%s\n", s);
>   return 0;
> }
> $ gcc -Wall -Werror -o t t.c && ./t
> cc1: warnings being treated as errors
> t.c: In function 'func':
> t.c:5:3: error: array subscript has type 'char'
> $ gcc -Wall -Werror -D_CTYPE_NOINLINE -o t t.c && ./t
> TEST STRING
> 
> With my proposed change the function and the macro both act the same.

and your program dumps core because it is invalid.

If your code is in EBCDIC (not ASCII) then "Test string" is (I think):
{ 0xe3, 0x85, 0xa2, 0xa3, 0x40, 0xa2, 0xa3, 0x99, 0x89, 0x95, 0x87, 0 }
and you keep passing invalid values to toupper() - with undefined
results (which is allowed to include 'format the hard drive').

FWIW toupper() used to only be defined on values where islower()
returned 'non-zero', SYSV used to use the same array for toupper()
and tolower().

        David

-- 
David Laight: david%l8s.co.uk@localhost


Home | Main Index | Thread Index | Old Index