C编程警告:数组下标的类型为'char'[-Wchar-subscripts]

1 c arrays gcc char subscript

我似乎无法解决这个问题.以下是我的代码:

#include<stdio.h>
#include<ctype.h>
#include<string.h>

_Bool are_anagrams (const char *word1, const char *word2);

int main (void)
{
    char an1[30], an2[30];
    int j;
    printf("Enter first word: ");
    scanf("%s", an1);
    printf("Enter second word: ");
    scanf("%s", an2);
    printf("The words are");

    j = are_anagrams (an1, an2);

    if (j == 0)
    {
        printf(" not anagrams. \n");
    }else
        printf(" anagrams. \n");

    return 0;
}

_Bool are_anagrams (const char *word1, const char *word2)
{
    int i;
    int check[26] = {0};
    for(i=0; i<30; i++)
        if(word1[i] == '\0')
            i=40;
        else
        {
            word1[i] = toupper(word1[i]);
            check[word1[i]-65]++;
        }

    for(i=0; i<30; i++)
        if(word2[i] == '\0')
            i=40;
        else
        {
            word2[i] = toupper(word2[i]);
            check[word2[i]-65]--;
        }

    for(i=0; i<26; i++)
        if(check[i] != 0)
        {
            return 0;
        }

    return 1;
}
Run Code Online (Sandbox Code Playgroud)

这些是错误消息:

anagram1.c:38:3: warning: array subscript has type ‘char’ [-Wchar-subscripts]
   word1[i] = toupper(word1[i]);
   ^
anagram1.c:38:3: error: assignment of read-only location ‘*(word1 + (sizetype)((long unsigned int)i * 1ul))’
anagram1.c:46:4: warning: array subscript has type ‘char’ [-Wchar-subscripts]
    word2[i] = toupper(word2[i]);
    ^
anagram1.c:46:4: error: assignment of read-only location ‘*(word2 + (sizetype)((long unsigned int)i * 1ul))’
Run Code Online (Sandbox Code Playgroud)

Mah*_*mer 5

警告:

warning: array subscript has type ‘char’
Run Code Online (Sandbox Code Playgroud)

是'toupper()'的结果,需要'int'类型作为参数,而问题代码提供'char'类型.

word1[i] = toupper(word1[i]);
...
word2[i] = toupper(word2[i]);
Run Code Online (Sandbox Code Playgroud)

要消除警告,请给toupper()'int'值:

word1[i] = toupper((unsigned char)word1[i]);
...
word2[i] = toupper((unsigned char)word2[i]);
Run Code Online (Sandbox Code Playgroud)

要彻底,您可以将'toupper()'返回的值从'int'转换回'char':

word1[i] = (char)toupper((unsigned char)word1[i]);
...
word2[i] = (char)toupper((unsigned char)word2[i]);
Run Code Online (Sandbox Code Playgroud)

错误:

error: assignment of read-only location
Run Code Online (Sandbox Code Playgroud)

是尝试使用'const'标志修改值的结果:

_Bool are_anagrams (const char *word1, const char *word2)
Run Code Online (Sandbox Code Playgroud)

如果合适,您可以通过消除'const'标志来消除错误:

_Bool are_anagrams (char *word1, char *word2)
Run Code Online (Sandbox Code Playgroud)

或者,您可以制作'const'字符串的本地工作副本:

_Bool are_anagrams (const char *I__word1, const char *I__word2)
   {
   int rCode = 0;
   int i;
   int check[26] = {0};
   char *word1 = strdup(I__word1);
   char *word2 = strdup(I__word2);

   for(i=0; i<30; i++)
      if(word1[i] == '\0')
         i=40;
      else
         {
         word1[i] = toupper(word1[i]);
         check[word1[i]-65]++;
         }

   for(i=0; i<30; i++)
      if(word2[i] == '\0')
         i=40;
      else
         {
         word2[i] = toupper(word2[i]);
         check[word2[i]-65]--;
         }

   for(i=0; i<26; i++)
      if(check[i] != 0)
        goto CLEANUP;

   rCode=1;

CLEANUP:
   free(word2);
   free(word1);

   return(rCode);
   }
Run Code Online (Sandbox Code Playgroud)

注意:上面的代码使用问题代码正文,这可能是也可能不准确.这个答案无意解决问题代码中的其他问题; 只是为了演示一种正确的方法来解决参数上的'const'标志,方法是创建参数的非'''''复制副本