Array subscript has type ‘char’ [-Wchar-subscripts]

后端 未结 4 2093
心在旅途
心在旅途 2021-01-21 04:52

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

4条回答
  •  温柔的废话
    2021-01-21 05:21

    -Wchar-subscripts : Warn if an array subscript has type char

    Example code which produces above warning.

    #include
    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
    #include
    #include  //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;
    }
    

提交回复
热议问题