Array subscript has type ‘char’ [-Wchar-subscripts]
Solution 1
The warning may be because you're passing a char
to the isspace()
macro. The implementation of isspace()
may be via array indexing. (The argument to the isspace()
macro is defined to be an integer the value of which can fit in an unsigned
char
).
I've just looked at ctype.h
in GNU libc, and while there's a complicated mess of macros and functions, somewhere in the definition of isspace()
there's an array being indexed.
See if isspace((unsigned char) *str)
gets rid of the warning.
Solution 2
The code you posted doesn't have any array subscripting at all, but that error is caused by code like this:
int array[256];
char c = ...;
array[c] = ...;
The line array[c]
is very likely a bug, because the type char
can be signed or unsigned—it's up to the compiler. If char
is signed, then it's possible for c
to be negative, in which case accessing a negative array index leads to Undefined Behavior.
The fix is to make sure you never use a char
variable as an array index—always cast to an unsigned type first, and always make sure that your value is in range. If you're running on a system with 8-bit char
s (i.e. CHAR_BIT
is 8), then a 256-element array will always be able to store all possible unsigned char
values, and you can omit the bounds check:
int array[256];
char c = ...;
array[(unsigned char)c] = ...; // Always ok, if CHAR_BIT <= 8
Solution 3
You can disable this warning with that compiler flag:
-Wno-char-subscripts
Related videos on Youtube

Admin
Updated on June 16, 2022Comments
-
Admin 6 months
I am trying to remove leading/trailing whitespace characters with help below helper function. When compiling i am getting warning: array subscript has type ‘char’ [-Wchar-subscripts] how to get rid of this message.
char *removeSpace(char *str ) { char *end; // Trim leading space while(isspace(*str)) str++; if(*str == 0) // All spaces? return str; // Trim trailing space end = str + strlen(str) - 1; while(end > str && isspace(*end)) end--; // Write new null terminator *(end+1) = 0; return str; }
-
Yuushi over 9 yearspossible duplicate of Warning: array subscript has type char
-
Paul over 9 yearsThat error doesn't come from the code you posted.
-
paddy over 9 yearsWhen you get warnings or errors, the compiler helpfully tells you which line number generated the problem. Most of the time, the compiler gets this right. The onus is on you to go to that line number and look at the code. If you don't understand it, you need to point out to us which line has the problem, and supply all relevant code that will help us explain it to you.
-
-
Steve Barnes over 9 yearsSurely it is better to always declare array indexing values as
unsigned
types! -
This isn't my real name over 9 yearsActually, the
isspace()
macro may be implemented using an array. -
Adam Pierce over 7 yearsYes that does get rid of the warning.
-
Roland Illig over 2 yearsDisabling a warning is only ever appropriate after you have completely understood the purpose of the warning.