Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #163731
| From | Meredith Montgomery <mmontgomery@levado.to> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: on ``warning: array subscript has type ‘char’'' |
| Date | 2021-12-07 15:11 -0300 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <86czm8s2sx.fsf@levado.to> (permalink) |
| References | <86mtldsbzf.fsf@levado.to> <87v901bfp3.fsf@nosuchdomain.example.com> |
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes: > Meredith Montgomery <mmontgomery@levado.to> writes: >> I can't explain this. >> >> %make warning >> gcc -Wall -x c -g -std=c99 -pedantic-errors warning.c -o warning >> In file included from warning.c:2: >> warning.c: In function ‘getop’: >> warning.c:38:27: warning: array subscript has type ‘char’ [-Wchar-subscripts] >> 38 | while (isdigit(s[++i] = c = getch())) >> | ~~~~~~~^~~~~~~~~~~~~ >> >> I understand the warning is trying to tell me that a char is not a good >> idea as an array subscript as a char could be negative. However, the >> array subscript is /i/, not /c/. So I'm puzzled. Thank you! > [...] > > The isdigit() function is probably implemented as a macro that uses > array indexing. (All library functions can also be implemented as > macros.) The part of the compiler that generates the warning message > doesn't know that the array indexing operator is the result of a macro > expansion, so it warns you as if you had written the indexing operator > directly. > > If you replace `isdigit` by `(isdigit)` or precede the call by > #undef isdigit > it will bypass the macro and call the actual function, and you probably > won't see the warning. I suggest this as a diagnostic method, not as a > solution. > > Having said that, the warning is quite useful even if it's less than > clear. The argument to isdigit() is of type int, and is required to be > either within the range of unsigned char *or* equal to EOF (which is > typically -1). Which means, annoyingly, that a char value passed to > isdigit() and friends should be cast to unsigned char. Passing a > negative char value (other than EOF) to isdigit() has undefined behavior. Lol! That's the C language we love! Order is back in the house now. Thank you so much for the great info. It did not occur to me that /isdigit/ could be macro. I will probably never forget this anymore. Thank you!
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
on ``warning: array subscript has type ‘char’'' Meredith Montgomery <mmontgomery@levado.to> - 2021-12-06 17:40 -0300
Re: on ``warning: array subscript has type ‘char’'' Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-06 13:12 -0800
Re: on ``warning: array subscript has type ‘char’'' Meredith Montgomery <mmontgomery@levado.to> - 2021-12-07 15:11 -0300
csiph-web