2

问题是这个警告在 15 和 18 警告:array subscript has type 'char' [-Wchar-subscripts]

处理样本输入:他们是学生。aeiou 样本输出:Thy r stdnts。

#include <cstdio>
#include <cstring>
const int MAXN = 10005;
char str1[MAXN], str2[MAXN];
bool HashTable[128] = {false};
//use HashTable to record the item need to minus

int main()
{
    fgets(str1, sizeof(str1), stdin);
    fgets(str2, sizeof(str2), stdin);
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    for (int i = 0; i < len2; i++) {
        HashTable[str2[i]] = true;
    }
    for (int i = 0; i < len1; i++) {
        if (HashTable[str1[i]] == false) {
            printf("%c", str1[i]);
        }
    }
    return 0;
}

我可以运行它,但我不知道警告。

这里

4

1 回答 1

1

铸造chartoint或提升它,例如,用一元加号将消除你的警告。

请注意,如果用户可以输入他们想要的任何内容(而且他们通常可以),那么您将面临越界访问的风险,因为您可能会得到一个大于127if charis unsigned 或负值 if charis signed 的值。

最安全的策略是将数组扩展为大小256并将 char 转换为,unsigned char然后再将其用作索引。

于 2019-06-08T09:56:13.447 回答