What does “typedef void (*Something)()” mean

后端 未结 5 508
星月不相逢
星月不相逢 2020-12-02 14:38

I am trying to understand what this means, the code I am looking at has

in .h

typedef void (*MCB)();
static MCB     m_process;

in .

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 14:52

    It's a function pointer. You get a SEGMENTATION FAULT because you are trying to make a call to a function which address is invalid (NULL).

    According to your specific sample, the function should return no value (void) and should receive no parameters ().

    This should work:

    void a()
    {
        printf("Hello!");
    }
    
    int main(int arcg, char** argv)
    {
        m_process = a;
        m_process(); /* indirect call to "a" function, */
        // Hello!
    }
    

    Function pointers are commonly used for some form of event handling in C. It's not its only use though...

提交回复
热议问题