gpt4 book ai didi

c - 警告 : array subscript has type char

转载 作者:太空狗 更新时间:2023-10-29 16:20:54 24 4
gpt4 key购买 nike

当我运行这个程序时,我收到警告“数组下标的类型为‘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();
}

最佳答案

简单,改变

char j;

unsigned char j;

或者只是一个普通的(u)int

unsigned int j;
int j;

来自 GCC Warnings

-Wchar-subscripts Warn if an array subscript has type char. This is a common cause of error, as programmers often forget that this type is signed on some machines. This warning is enabled by -Wall.

编译器不希望您无意中指定负数组索引。因此警告!

关于c - 警告 : array subscript has type char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com