【发布时间】:2015-02-13 23:47:22
【问题描述】:
当我使用gcc -Wall 运行我的程序时,我得到了
warning: array subscript has type ‘char’
请帮助我哪里出了问题。警告说它在第 20:7 和 21:7 行。我怎样才能摆脱警告?
/* This program asks the user for 2 words and tells the user if those 2 words
are anagrams even if its capitalized or not. The maximum length of letters
is 20. */
#include<stdio.h>
#include<string.h>
int anagram(char* str1, char* str2)
{
// Create two count arrays and initialize all values as 0
int numOfChar = 20;
char count1[123] = {0};
char count2[123] = {0};
int i;
/* For each character in the strings, it increases in
the corresponding count array */
for (i = 0; str1[i] && str2[i]; i++)
{
count1[str1[i]]++;
count2[str2[i]]++;
}
// If both strings are different lengths.
if (str1[i] || str2[i]) // If one statement is true
{
return 0;
}
// Compares count arrays
for (i = 0; i < numOfChar; i++)
{
if (count1[i] != count2[i]) // If dont equal to eachother
{
return 0;
}
}
return 1;
}
// Construct function
void construct()
{
int anagram(char*,char*); // Variables
char str[20], str1[20];
int check = 0;
printf("Please enter the first word: ");
scanf("%s", str);
printf("Please enter the second word: ");
scanf("%s", str1);
check=anagram(str, str1);
if (check==1)
{ // If true then print
printf("%s is an anagram of %s\n", str, str1);
}
else
{ // If false then print
printf("%s is NOT an anagram of %s\n", str, str1);
}
return ;
}
// Main function
int main()
{
construct(); // Calls construct function
return 0;
}
【问题讨论】:
-
您可以将
chars 转换为int或size_t -
快速修复
count1[(int)str1[i]]++; -
我已经编辑了你的标题。标题是一条错误消息,但您将其更改为混合大小写。 C 和 gcc 命令行选项都区分大小写;
Char与char不同。
标签: c arrays string char gcc-warning