I have an array that occasionally grows as I add more states. I want to be sure that I have initialized all the array members. (These examples are simplistic, my array is actually an array of structs that keep track of a system state.)
Example one, no programming errors, compiles fine:
#define NUM_HEX_LETTERS 16
const char *Hex_Letters_As_Strings[NUM_HEX_LETTERS ]=
{
"0=0",
"1=1",
"2=2",
"3=3",
"4=4",
"5=5",
"6=6",
"7=7",
"8=8",
"8=9",
"10=A",
"11=B",
"12=C",
"13=D",
"14=E",
"15=F",
};
Example two, extra initializer, throws: "error: too many initializers for 'const char* [16]" as expected:
#define NUM_HEX_LETTERS 16
const char *Hex_Letters_As_Strings[NUM_HEX_LETTERS ]=
{
"0=0",
"1=1",
"2=2",
"3=3",
"4=4",
"5=5",
"6=6",
"7=7",
"8=8",
"8=9",
"10=A",
"11=B",
"12=C",
"13=D",
"14=E",
"15=F",
"16=G", //Whups -- that is for base 17 !
};
Example three: Too few initializers. No error. Is there a way to detect this at compile time?
#define NUM_HEX_LETTERS 16
const char *Hex_Letters_As_Strings[NUM_HEX_LETTERS ]=
{
"0=0",
"1=1",
"2=2",
"3=3",
"4=4",
"5=5",
"6=6",
"7=7",
"8=8",
"8=9",
"10=A",
"11=B",
"12=C",
"13=D",
"14=E",
//Missing initialization for 1 array member. No error. How can I detect this ?
};
I tried
#pragma GCC diagnostic error "-Wmissing-field-initializers"
and
#pragma GCC diagnostic error "-Wuninitialized"
but neither seems to detect this kind of programming error.
Is there a way to pick up this kind of dumb programming error?