what does null function pointer in C mean?

后端 未结 2 363
梦如初夏
梦如初夏 2021-01-21 15:24

Say we have a function pointer:

void (*func0)(void);

which is also defined:

void func0(void) { printf( \"0\\n\" ); }

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-21 16:01

    It is undefined behaviour to de-reference a null pointer. The fix is simply to ensure that the pointer refers to an appropriate function.

    In your case you want something like this:

    void MyFunction(void)
    {
        printf( "0\n" ); 
    }
    

    and then later you can assign to func0:

    func0 = &MyFunction;
    

    Note that I am using a different name for the function pointer variable and the actual function.

    And now you can call the function, via the function pointer:

    func0();
    

提交回复
热议问题