【发布时间】:2021-03-19 00:33:31
【问题描述】:
当我运行这个程序时,我收到警告“数组下标的类型为 'char'”。 请帮助我哪里出错了。我正在使用 code::blocks IDE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void NoFive()
{
long long int cal;
char alpha[25];
char given[100] = "the quick brown fox jumped over the cow";
int num[25];
int i, k;
char j;
j = 'a';
k = 26;
cal = 1;
for(i = 0; i <= 25; i++)
{
alpha[i] = j++;
num[i] = k--;
// printf("%c = %d \n", alpha[i], num[i]);
}
for(i = 0; i <= (strlen(given) - 1); i++)
{
for(j = 0; j <= 25; j++)
{
if(given[i] == alpha[j]) ***//Warning array subscript has type char***
{
cal = cal * num [j]; ***//Warning array subscript has type char***
}
else
{
}
}
}
printf(" The value of cal is %I64u ", cal);
}
main()
{
NoFive();
}
【问题讨论】:
-
gcc.gnu.org/onlinedocs/gcc/Warning-Options.html 将阐明为什么这是一个警告。
-
for(i = 0; i <= 25; i++)也是错误的(两次)。应该是for(i = 0; i < 25; i++) {...}数组有 25 个元素。而for(i = 0; i <= (strlen(given) - 1); i++)值得商榷。 -
@ta.speot.is 不幸的是,GCC 文档没有任何说明为什么。它甚至没有试图解释这种情况。
-
@RolandIllig 它说 如果数组下标的类型为 char 则发出警告。这是错误的常见原因,因为程序员经常忘记这种类型是在某些机器上签名的。此警告由 -Wall 启用。 为什么要使用负下标?
-
@ta.speot.is 我不想要负下标,我隐含地得到它而不做任何事情。这就是问题所在。
标签: c codeblocks