woj*_*teo 6 c arrays initializer char
尝试用C语言编译程序时,我有一些警告:
Run Code Online (Sandbox Code Playgroud)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]
来源是:
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");
}
Run Code Online (Sandbox Code Playgroud)
我之后也有"核心倾销" scanf("%d", &b);,但int b=0;不会出问题......
这里有两个错误:首先,您尝试声明arrays[63]存储64个元素,因为您可能会将array(n)的大小与最大可能的索引值(即)进行混淆n - 1.所以它绝对应该litera[64]和liczba[64].顺便说一句,你也必须改变这一行 - while (i<=64)否则你最终会尝试访问第65个元素.
第二,你试图char用%sscanf的格式说明符填充值,而你应该在%c这里使用.
另外,不禁想知道为什么将liczba数组声明为存储ints的数组,用chars 数组初始化数组.所有这些'1','2'等...文字代表不是相应的数字 - 但它们的字符代码.我怀疑那是你的意图.