Initializing an array (in C++, but any solution which works for C will likely work here as well) with less initializers than it has elements is perfectly legal:
int array[10] = { 1, 2, 3 };
However, this can be a source of obscure bugs. Is there a way to have the compiler (gcc) check the number of initializers for one specific array, and emit a warning or even an error if declared and actual size don't match?
I know I can use int array[] = { 1, 2, 3 };
and could then use static assertions involving sizeof(array)
to verify my expectation there. But I'm using array
in other translation units, so I have to declare it with an explicit size. So this trick won't work for me.