[英]C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’
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]
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);
在
scanf("%d", &b);
之后我也有“核心转储” scanf("%d", &b);
, but int b=0;
, 但
int b=0;
doesn't make a problem...不会有问题...
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
).这里有两个错误:首先,您试图声明
arrays[63]
来存储 64 个元素,因为您可能将数组 ( n
) 的大小与最大可能的索引值(即n - 1
)混淆了。 So it definitely should be litera[64]
and liczba[64]
.所以它绝对应该是
litera[64]
和liczba[64]
。 BTW, you have to change this line too - while (i<=64)
: otherwise you end up trying to access 65th element.顺便说一句,您也必须更改此行 -
while (i<=64)
:否则您最终会尝试访问第 65 个元素。
And second, you're trying to fill char
value with %s
format specifier for scanf, while you should have used %c
here.其次,您试图用
%s
格式说明符为 scanf 填充char
值,而您应该在这里使用%c
。
Also, can't help wondering why you declare liczba
array as one that stores int
s, that initialize it with array of char
s.此外,不禁想知道为什么将
liczba
数组声明为存储int
的数组,并使用char
数组对其进行初始化。 All these '1', '2', etc... literals represent NOT the corresponding digits - but the charcodes for them.所有这些“1”、“2”等……文字代表的不是相应的数字——而是它们的字符代码。 I doubt that was your intent.
我怀疑那是你的意图。
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 .用 64 代替数组大小初始化。 initialize 1 more index when you are initializing the size of array.
初始化数组大小时再初始化 1 个索引。
索引总是从 i = 0 开始,但元素计数从 1 开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.