C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’

wojteo picture wojteo · Nov 25, 2012 · Viewed 70.4k times · Source

I've some warnings while trying to compile program in C language:

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...

Answer

raina77ow picture raina77ow · Nov 25, 2012

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 65th element.

And second, you're trying to fill char value with %s format specifier for scanf, while you should have used %c here.

Also, can't help wondering why you declare liczba array 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.