C语言 C:警告:数组初始值设定项中的元素过多;'xxx' 即将初始化;需要“char *”,但类型为“int”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13549933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’
提问by wojteo
I've some warnings while trying to compile program in C language:
我在尝试用 C 语言编译程序时有一些警告:
13:20: warning: excess elements in array initializer [enabled by default] 13:20: warning: (near initialization for ‘litera') [enabled by default] 22:18: warning: excess elements in array initializer [enabled by default] 22:18: warning: (near initialization for ‘liczba') [enabled by default] 43:1: warning: format ‘%s' expects argument of type ‘char *', but argument 2 has type ‘int' [-Wformat] 45:2: warning: format ‘%s' expects argument of type ‘char *', but argument 2 has type ‘int' [-Wformat] 55:2: warning: format ‘%s' expects argument of type ‘char *', but argument 2 has type ‘int' [-Wformat]
13:20: warning: excess elements in array initializer [enabled by default] 13:20: warning: (near initialization for ‘litera') [enabled by default] 22:18: warning: excess elements in array initializer [enabled by default] 22:18: warning: (near initialization for ‘liczba') [enabled by default] 43:1: warning: format ‘%s' expects argument of type ‘char *', but argument 2 has type ‘int' [-Wformat] 45:2: warning: format ‘%s' expects argument of type ‘char *', but argument 2 has type ‘int' [-Wformat] 55:2: warning: format ‘%s' expects argument of type ‘char *', but argument 2 has type ‘int' [-Wformat]
The source is:
来源是:
char litera[63] = {'E', 'G', 'H', 'F', 'E', 'G', 'H', 'F',
'D', 'B', 'A', 'C', 'D', 'B', 'A', 'C',
'B', 'A', 'C', 'D', 'C', 'A', 'B', 'D',
'F', 'H', 'G', 'E', 'F', 'H', 'G', 'E',
'C', 'D', 'B', 'A', 'B', 'A', 'C', 'D',
'F', 'E', 'G', 'H', 'G', 'H', 'F', 'G',
'H', 'F', 'E', 'F', 'H', 'G', 'E', 'C',
/*line13:*/ 'A', 'B', 'D', 'C', 'A', 'B', 'D', 'E'};
int liczba[63] ={'8', '7', '5', '6', '4', '3', '1', '2',
'1', '2', '4', '3', '5', '6', '8', '7',
'5', '7', '8', '6', '4', '3', '1', '2',
'1', '2', '4', '3', '5', '6', '8', '7',
'6', '8', '7', '5', '3', '1', '2', '4',
'3', '1', '2', '4', '6', '8', '7', '5',
'7', '8', '6', '4', '3', '1', '2', '1',
/*line22*/ '2', '4', '3', '5', '6', '8', '7', '5'};
int i=0, x=0, n;
char a;
int b=0;
printf("Podaj liter? z pola startowego skoczka(du?e litery)\n");
scanf("%s", &a);
printf("Podaj liczb? z pola startowego skoczka \n");
scanf("%d", &b);
if (b<=0 || b>8){
printf("Z?a pozycja skoczka\n");
return 0;
}
while (litera[i] != a || liczba[i] != b){
++i;
}
n=i;
/*line43*/ printf("Trasa skoczka od punktu %s %d to:\n", a, b);
while (i<=64){
/*line45*/ printf("%s%d ", litera[i], liczba[i]);
++i;
++x;
x=x%8;
if(x==0)
printf("/n");
}
i=0;
while (i<n){
/*line55*/ printf("%s%d ", litera[i], liczba[i]);
++i;
++x;
x=x%8;
if(x==0)
printf("/n");
}
also I have "core dumped" after scanf("%d", &b);, but int b=0;doesn't make a problem...
之后我也有“核心转储” scanf("%d", &b);,但int b=0;没有问题......
回答by raina77ow
Two errors here: first, you're trying to declare arrays[63]for storing 64 elements, as you've probably confused the size of array (n) with the maximum possible index value (that's n - 1). So it definitely should be litera[64]and liczba[64]. BTW, you have to change this line too - while (i<=64): otherwise you end up trying to access 65thelement.
这里有两个错误:首先,您试图声明arrays[63]存储 64 个元素,因为您可能将数组 ( n)的大小与最大可能的索引值(即n - 1)混淆了。所以它绝对应该是litera[64]and liczba[64]。顺便说一句,您也必须更改此行 - while (i<=64):否则您最终会尝试访问第 65 个元素。
And second, you're trying to fill charvalue with %sformat specifier for scanf, while you should have used %chere.
其次,您尝试char使用%sscanf 的格式说明符填充值,而您应该在%c这里使用。
Also, can't help wondering why you declare liczbaarray as one that stores ints, that initialize it with array of chars. All these '1', '2', etc... literals represent NOT the corresponding digits - but the charcodes for them. I doubt that was your intent.
此外,不禁想知道为什么将liczba数组声明为存储ints 的数组,并使用chars数组对其进行初始化。所有这些“1”、“2”等……文字代表的不是相应的数字——而是它们的字符代码。我怀疑那是你的意图。
回答by Nazim
char litera[63] -> char litera[64] int liczba[63] -> int liczba[64]
字符字面量[63] -> 字符字面量[64] int liczba[63] -> int liczba[64]
make 64 in the place of array size initialization . initialize 1 more index when you are initializing the size of array.
用 64 代替数组大小初始化。初始化数组大小时,再初始化 1 个索引。

