c - Checking enum values at compile time -
i'd check static initalizers @ compile time. i'm implementing macro cassert() this question.
now, have "strange" situation
typedef enum { eqadc_chan_a_00 = 0, eqadc_chan_a_01, eqadc_chan_a_02, eqadc_chan_a_03, eqadc_chan_a_04, ... // others eqadc_chan_max // eqadc_chan_max = 62 } eqadc_tinputbiosid;
and have structure initialized this:
const t_eqadc_pin_config eqadc_xpinconfig[eqadc_chan_max] = { { 123 }, /* eqadc_chan_a_00 */ { 321 }, /* eqadc_chan_a_01 */ ... /* others */ };
what strange (to me...) following statement
cassert(( sizeof(eqadc_xpinconfig)/sizeof(eqadc_xpinconfig[0]) ) != 62 );
works fine, , "passes" (i.e. compiles without errors). instead, this:
cassert(( sizeof(eqadc_xpinconfig)/sizeof(eqadc_xpinconfig[0]) ) != eqadc_chan_max );
does not (i.e. generates assertion, , stops compiler.
trying figure out why happens, think problem related value of eqadc_chan_max, not known @ compile time, being enum value. if case, still can't understand why declaration
const t_eqadc_pin_config eqadc_xpinconfig[eqadc_chan_max]
actually instantiates correct size array... on how can (better) implement appreciated.
edit: fault. correct syntax is
cassert(( sizeof(eqadc_xpinconfig)/sizeof(eqadc_xpinconfig[0]) ) == eqadc_chan_max );
furthermore, pay attention declaring array way:
const t_eqadc_pin_config eqadc_xpinconfig[eqadc_chan_max] = { initializers ....}
actually allocates size of eqadc_chan_max elements, if number of initialized elements not correct. so, right way implement is:
const t_eqadc_pin_config eqadc_xpinconfig[] = { initializers ....} cassert(( sizeof(eqadc_xpinconfig)/sizeof(eqadc_xpinconfig[0]) ) == eqadc_chan_max );
thank all.
have @ this thread.
if doesn't can run compilation step -p option, or whatever compiler, produce pre-processor output? might clarify why predicate in 2nd assertion doesn't evaluate expect.
also, why asserting size of array not 62?
Comments
Post a Comment