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

前端 未结 5 1914
广开言路
广开言路 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条回答
  •  醉话见心
    2021-01-30 09:57

    In situations like this, do a typedef to name your function signature, that makes it far simpler:

    typedef void MESSAGE_HANDLER(void);
    

    with that in place, it should be just:

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

    To get the actual content of the array constant.

    EDIT: Removed pointer part from the typedef, this really is better (live and learn).

提交回复
热议问题