How does one declare an array of constant function pointers in C?

前端 未结 5 1925
广开言路
广开言路 2021-01-30 09:28

I need to declare an array of pointers to functions like so:

extern void function1(void);
extern void function2(void);
...

void (*MESSAGE_HANDLERS[])(void) = {
         


        
5条回答
  •  旧时难觅i
    2021-01-30 10:04

    I am not sure if this will work in 'C'. it does work in 'C++':

    • First define MESSAGE_HANDLERS as a type:

      typedef void (*MESSAGE_HANDLER)();

    • Then, use the type definition to declare your array a constant:

      MESSAGE_HANDLER const handlers[] = {function1, function2};

    The trick is in the typedef, if you can do the same semantically in 'C', it should work too.

提交回复
热议问题