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-subsc
You can disable this warning with that compiler flag:
-Wno-char-subscripts
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.
-Wchar-subscripts : Warn if an array subscript has type char
Example code which produces above warning.
#include<stdio.h>
int main()
{
int a[10]; //or even char a[10];
char c=5;
printf("%d",a[c]); //you might used like this in your code array with character subscript
return 0;
}
In your code(Which is not posted), You might used char subscript As Like above.
Example of main which produces the -Wchar-subscripts warning in your code:
int main()
{
char c=20;
char s[c];
printf("Enter string:");
fgets(s,sizeof(s),stdin);
printf("String before removal of spaces : \n%s",s);
printf("%c",s[c]); //accessing s[c],array element with char subscript or else any Expression you might used this
printf("test text\n");
strcpy(s,removeSpace(s));
printf("String after removal of spaces : \n%s",s);
printf("test text\n");
return 0;
}
But there is No char subscript , in the code which you have posted. tested your code by adding main. did not reproduce any warning or error.I did compilation with gcc -Wall -Werror file.c .
if you still did not figure , post your whole code.
test Code Which did not produce any warnings and errors:
#include<stdio.h>
#include<string.h>
#include <ctype.h> //included this to use isspace
char *removeSpace(char *);
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;
}
int main()
{
char s[20];
printf("Enter string:");
fgets(s,sizeof(s),stdin);
printf("String before removal of spaces : \n%s",s);
printf("test text\n");
strcpy(s,removeSpace(s));
printf("String after removal of spaces : \n%s",s);
printf("test text\n");
return 0;
}
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