Warning: excess elements in array initializer

The compiler throws the warning called excess elements in array initializer because the size of the array didn’t match the no of elements in an array

Let’s see an example

int array[3] = {25, 50, 75, 100};

As can be seen from the above array declaration that the size of the array is 3 but we initialized it with 4 values

In reality, it is the elements that are exceeded the size range of an array that you declared

Furthermore, if you don’t declare the size of the array then, in that case, there won’t be any warning of excess elements in the array initialize

int array[] = {25, 50, 75, 100};

Thereafter, the same concept is also applied to 2D array

int array_2d[2][3] = { {1, 4, 2,6}, {3, 6, 8,2} };

as can be seen, from the  above 2d_array declaration that it has 2 rows and 3 columns but the elements are exceeded the size limit

the total elements of the above 2d_array  should be 2*3=6 but the above 2d_array declaration has 8 elements in it

so, that means it is a mismatch of the declared size of the array_2d and the no of elements of an array_2d as a result, the compiler will throw a warning called excess elements in the initializer

similarly, the concept is also applied to 3D array

 // initializing the 3-dimensional array
    int array_3d[2][3][2] = { { { 0, 1,2 }, { 2, 3 }, { 4, 5 } },
                       { { 6, 7 }, { 8, 9 }, { 10, 11 } } };

as you see from the above 3D array declaration in the 1block the first row has  3 columns

that’s a mismatch with the array_3d[2][3][2] declaration size

from the above array_3d[2][3][2] declaration the total elements should be 2*3*2=12 but we initialized with 13 elements which means it is a mismatch with the size of  the 3D array and its initialized elements

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.