Home > OS >  warning: excess elements in array initializer
warning: excess elements in array initializer

Time:05-26

I'm trying to create 2D array.

int main() {
    int stalagmite[10][6] = { { 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 },
                              { 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 },
                              { 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
                              { 1, 1, 0, 1, 1, 1, 0, 1, 0, 0 },
                              { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 },
                              { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 } };

    printf("%d\n", stalagmite[3][3]);
}

Output as follows:

deneme.c:9:51: note: (near initialization for ‘stalagmite[5]’)
deneme.c:9:54: warning: excess elements in array initializer
9 |                         { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 } };

Trying to define an array with initializer. When I try to compile it always return a error stated above. I tried to change size, data type but it had no effects.

CodePudding user response:

int stalagmite[10][6] defines an array of 10 arrays of 6 int, whereas your initializer specifies 6 arrays of 10 integers.

The definition should probably be changed to int stalagmite[6][10] = {...} or possibly int stalagmite[][10] = {...}.

  • Related