// и нечего массивы не того типа заводить да приведения делать
typedef void (*pvoidfunc_t)(void);
void func1(void) { }
void func2(void) { }
// ну там флеш и прочие модификаторы по вкусу
const pvoidfunc_t functions[] = { func1, func2 };
void array_call(unsigned char index)
{
pvoidfunc_t pfunc = functions[index];
(*pfunc)();
}
// собственно, стандарт С позволяют и так
void array_call_1(unsigned char index)
{
pvoidfunc_t pfunc = functions[index];
pfunc(); // без *-ки
}
// и даже так
void array_call_2(unsigned char index)
{
functions[index]();
}
Для AVR все три варианта компилируются в одинаковый код.
.text
func1:
ret
func2:
ret
array_call:
mov r30,r24
ldi r31,lo8(0)
lsl r30
rol r31
subi r30,lo8(-(functions))
sbci r31,hi8(-(functions))
ld __tmp_reg__,Z+
ld r31,Z
mov r30,__tmp_reg__
icall
ret
array_call_1:
mov r30,r24
ldi r31,lo8(0)
lsl r30
rol r31
subi r30,lo8(-(functions))
sbci r31,hi8(-(functions))
ld __tmp_reg__,Z+
ld r31,Z
mov r30,__tmp_reg__
icall
ret
array_call_2:
mov r30,r24
ldi r31,lo8(0)
lsl r30
rol r31
subi r30,lo8(-(functions))
sbci r31,hi8(-(functions))
ld __tmp_reg__,Z+
ld r31,Z
mov r30,__tmp_reg__
icall
ret
.data
functions:
.word gs(func1)
.word gs(func2)