Newsgroups: comp.lang.c Path: utzoo!telly!problem!druid!darcy From: darcy@druid.uucp (D'Arcy J.M. Cain) Subject: Re: Pointer arithmetic Message-ID: <1991Jan6.045006.6260@druid.uucp> Organization: D'Arcy Cain Consulting, West Hill, Ontario References: <1991Jan5.001607.5915@demott.com> Date: Sun, 6 Jan 91 04:50:06 GMT [I removed comp.lang.c++ from the Newsgroups header as it seems to be only a C question] In article <1991Jan5.001607.5915@demott.com> Kevin D. Quitt writes: >const unsigned char *chars = "some string" >unsigned char pass[]; > c = strchr( chars, toupper( pass[ i ]) ) - chars + 1; > gcc would not accept this no matter what I did. I too have noticed this difference between gcc and Turbo-C. The problem seems to be the "unsigned" part of the declaration of chars. I tried the following: #include #include int main(void) { const char *chars = "some string"; int c; c = strchr(chars, 'e') - chars + 1; return(c); } which compiled without a peep with "gcc -O -Wall tst.c -o tst" but failed when I declared chars as "const unsigned char *chars ...". (it didn't seem to matter whether or not const was used.) The error message is: tst.c: In function main: tst.c:6: warning: initialization between incompatible pointer types tst.c:9: warning: argument passing between incompatible pointer types tst.c:9: invalid operands to binary - tst.c:7: warning: `c' may be used uninitialized in this function So gcc differentiates unsigned char from char. String literals are composed of char objects so their addresses can't be assigned to an unsigned char pointer, strchr() takes a char pointer so an uncasted unsigned char doesn't match the parameter list and char and unsigned char can't be subtracted. So the question is what does ANSI say on the issue. K&R2 seems to support gcc in this. (A2.6, page 194 and A6.6, page 198.) In any case, the snippet you provide doesn't exhibit any reason for using unsigned char over char so perhaps you can just change the declaration. -- D'Arcy J.M. Cain (darcy@druid) | D'Arcy Cain Consulting | There's no government West Hill, Ontario, Canada | like no government! +1 416 281 6094 |