4

我正在尝试在帮助函数下方的帮助下删除前导/尾随空格字符。编译时我收到警告:array subscript has type 'char' [-Wchar-subscripts] 如何摆脱此消息。

  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;
 }
4

4 回答 4

17

警告可能是因为您将 a 传递charisspace()宏。的实现isspace()可能是通过数组索引。(isspace()宏的参数被定义为一个整数,其值可以放在一个 中unsignedchar)。

我刚刚ctype.h在 GNU libc 中查看过,虽然有一堆复杂的宏和函数,但在定义的某个地方isspace()有一个被索引的数组。

看看是否isspace((unsigned char) *str)摆脱了警告。

于 2013-08-29T05:39:38.553 回答
7

您发布的代码根本没有任何数组下标,但该错误是由如下代码引起的:

int array[256];
char c = ...;
array[c] = ...;

该行array[c]很可能是一个错误,因为类型char可以是有符号或无符号的——这取决于编译器。如果char有符号,则可能为c负数,在这种情况下,访问负数数组索引会导致未定义行为。

解决方法是确保您从不将char变量用作数组索引——始终首先转换为无符号类型,并始终确保您的值在范围内。如果你在一个 8 位chars(即CHAR_BIT8 位)的系统上运行,那么一个 256 元素的数组将始终能够存储所有可能的unsigned char值,并且你可以省略边界检查:

int array[256];
char c = ...;
array[(unsigned char)c] = ...;  // Always ok, if CHAR_BIT <= 8
于 2013-08-29T04:30:26.367 回答
1

You can disable this warning with that compiler flag:

-Wno-char-subscripts
于 2013-11-07T14:58:43.833 回答
-1

-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;
}
于 2013-08-29T05:33:04.793 回答