警告:数组下标具有char类型 [英] Warning: array subscript has type char
问题描述
当我运行这个程序我得到警告数组下标有键入'符'。
请帮我这是去错在何处。我使用code ::块IDE
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&string.h中GT;
无效NoFive()
{
得到long long int CAL;
焦炭阿尔法[25];
鉴于烧焦[100] =快速的棕色狐狸跳过了牛;
INT NUM [25];
INT I,K;
焦炭焦耳;
J ='A';
K = 26;
CAL = 1;
对于(i = 0; I< = 25;我++)
{
阿尔法[I] = J ++;
NUM [I] = K--;
//的printf(%C =%d个\\ N,阿尔法[I],NUM [I]);
}
对于(I = 0; I&下; =(strlen的(给定) - 1);我+ +)
{
为(J = 0; J< = 25; J ++)
{
如果(鉴于[I] ==阿尔法[J]。)*** //警告数组下标具有char类型***
{
CAL = CAL * NUM [J]。 *** //警告:数组下标具有char类型***
}
其他
{ }
}
}
输出(校准的值是%I64u,CAL);
}主要()
{
NoFive();
}
简单,变化
字符焦耳;
到
unsigned char型焦耳;
或只是一个普通的(U)INT
无符号整型焦耳;
诠释J;
-Wchar-标警告如果数组下标具有char类型。这是错误的一个常见原因,如程序员往往忘记了这种类型
在某些机器上签署。这个警告是被-Wall启用。
块引用>编译器不会让你在不经意间指定负数组索引。因此警告!
When I am running this program I am getting warning "array subscript has type 'char'". Please help me where is it going wrong. I am using 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(); }
解决方案Simple, change
char j;
to
unsigned char j;
or to just a plain
(u)int
unsigned int j; int j;
From 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.
The compiler doesn't want you to inadvertantly specify a negative array index. And hence the warning!
这篇关于警告:数组下标具有char类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!