Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #154851
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Puzzling "array subscript has type ‘char’" warning. |
| Date | 2020-09-10 20:58 -0700 |
| Organization | None to speak of |
| Message-ID | <87lfhhge1m.fsf@nosuchdomain.example.com> (permalink) |
| References | <e7Cdncm5EpqKbMfCnZ2dnUU7_83NnZ2d@giganews.com> |
Robbie Hatley <see.my.signature@for.my.contact.info> writes:
> Ahoy, group. I just ran into a warning I don't understand.
> The code that generated the warning was this:
>
> else if (isdigit(*Ptr2))
> {
> *Ptr1++ = *Ptr2;
> FirstDigit = 1;
> }
>
> and the warning (from GCC) was this:
>
> integer-from-string.c:39:24: warning: array subscript has
> type ‘char’ [-Wchar-subscripts] 39 | else if (isdigit(*Ptr2))
>
> But there are no arrays being used! Ptr1 and Ptr2 are simple
> pointers-to-char. So why would GCC think that *Ptr2 is an
> array subscript?
>
> As I understand it, isdigit() (from "ctype.h") doesn't treat
> its argument as an array-subscript, but as an int character
> ordinal, and returns 1 if the character is a digit, otherwise
> returns 0.
isdigit is very likely a macro in the implementation you're using.
The macro presumably works by subscripting into an array object.
> I was able to make the warning go away with a typecast to int:
>
> else if (isdigit((int)(*Ptr2)))
> {
> *Ptr1++ = *Ptr2;
> FirstDigit = 1;
> }
>
> But oddly, the code works fine with EITHER version. So I'm not
> understanding where the "array subscript" warning is coming
> from. Anyone have any ideas?
The ctype() function requires an argument of type int whose value must
be either EOF (almost certainly -1) or a value representable as an
unsigned char. For any other value, the behavior is undefined.
A typical implementation might define an array of small integers indexed
from 0 to UCHAR_MAX+1, with the index value computed by subtracting 1
from the argument.
Given that Ptr2 is of type char*, if plain char is signed and *Ptr2
happens to be negative (and not equal to EOF), then evaluating
isdigit(*Ptr2) has undefined behavior. Converting to int merely
masks the problem. Your code "works fine" if *Ptr1 happens to
be non-negative.
Cast the argument to unsigned char:
isdigit((unsigned char)*Ptr2
(Yes, it's annoying that you need to do thsis, and I consider it a
design flaw in the <ctype.h> functions, but there it is.)
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Puzzling "array subscript has type ‘char’" warning. Robbie Hatley <see.my.signature@for.my.contact.info> - 2020-09-10 20:37 -0700
Re: Puzzling "array subscript has type ‘char’" warning. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-09-10 20:58 -0700
Re: Puzzling "array subscript has type ‘char’" warning. Robbie Hatley <see.my.signature@for.my.contact.info> - 2020-09-10 22:49 -0700
Re: Puzzling "array subscript has type ‘char’" warning. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-09-11 01:10 -0700
Re: Puzzling "array subscript has type ‘char’" warning. gazelle@shell.xmission.com (Kenny McCormack) - 2020-09-11 13:09 +0000
Re: Puzzling "array subscript has type ‘char’" warning. Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-09-11 06:14 -0700
Re: Puzzling "array subscript has type ‘char’" warning. Richard Damon <Richard@Damon-Family.org> - 2020-09-11 08:09 -0400
Re: Puzzling "array subscript has type ‘char’" warning. James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-09-11 09:01 -0400
Re: Puzzling "array subscript has type ‘char’" warning. Kaz Kylheku <793-849-0957@kylheku.com> - 2020-09-11 16:16 +0000
Re: Puzzling "array subscript has type char" warning. dave_thompson_2@comcast.net - 2020-10-11 16:12 -0400
Re: Puzzling "array subscript has type char" warning. Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-10-11 17:28 -0700
Re: Puzzling "array subscript has type char" warning. dave_thompson_2@comcast.net - 2020-11-01 23:07 -0500
Re: Puzzling "array subscript has type ‘char’" warning. jenniferjeson35@gmail.com - 2020-11-06 20:35 -0800
csiph-web