C: warning: redundant elements in the array initializer; near initialization for 'xxx'; expects 'char * but has type' int

I have some warnings when trying to compile a C program:

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] 

A source:

 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; no problem ...

+6
source share
1 answer

Two errors are here: firstly, you are trying to declare arrays[63] to store 64 elements, since you are probably confusing the size of the array ( n ) with the maximum possible index value ( n - 1 ), therefore it should definitely be litera[64] and liczba[64] . BTW, you must also change this line - while (i<=64) : otherwise you will try to access the 65th element.

And secondly, you are trying to fill the char value of the %s format specifier for scanf, whereas you should use %c here.

It is also not surprising why you declare an array of liczba as one that stores int s, which initializes it with an array of char s. All these "1", "2", etc ... literals are NOT the corresponding numbers - but the characters for them. I doubt it was your intention.

+5
source

All Articles