简体   繁体   English

C:警告:数组初始值设定项中的元素过多; 'xxx' 即将初始化; 需要“char *”,但类型为“int”

[英]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.

相关问题 警告:格式&#39;%c&#39;需要类型&#39;int&#39;,但参数2的类型为&#39;char *&#39; - warning: format '%c' expects type 'int', but argument 2 has type 'char *' char数组初始化程序中的多余元素 - excess elements in char array initializer 警告:数组初始值设定项中的多余元素 - warning: excess elements in array initializer C-“警告:格式”%c”期望参数类型为“ char *”,但是参数3的类型类型为“ int”: - C - “warning: format ”%c“ expects argument of type ”char *“, but argument 3 has type ”int":? char数组初始值设定项错误中的多余元素 - Excess elements in char array initializer error 警告:格式&#39;%s&#39;需要类型&#39;char *&#39;,但参数2的类型为&#39;int&#39; - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’ 编译警告。数组初始化程序中的多余元素 - Compilation Warning.Excess elements in array initializer 错误:char数组初始化程序中的多余元素 - error: excess elements in char array initializer 警告:格式%s期望为char *类型,但参数2为int类型 - warning: format %s expects type char * but argument 2 has type int 警告:格式&#39;%c&#39;需要&#39;char *&#39;类型的参数,但参数3的类型为&#39;int&#39; - warning: format '%c' expects argument of type 'char *', but argument 3 has type 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM