什么用C空函数指针是什么意思? [英] what does null function pointer in C mean?

查看:192
本文介绍了什么用C空函数指针是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个函数指针:

Say we have a function pointer:

void (*func0)(void);

其也被定义为:

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

但是说,在某些时候,我们试图以某种方式访问​​函数指针,那么如果MS VS调试器显示func0实际指向为0x0000,当我踏进code,这是什么意思?而且,也请让我知道什么是最好的解决?谢谢你。

But say, at some point we try to access the function pointer somehow, then if the MS VS debugger shows that func0 actually points to 0x0000 when I step into the code, what does it mean? And also, please also let me know what is the best fix? Thanks.

推荐答案

这是不确定的行为,反引用一个空指针。解决方法是简单地确保指针指到适当的功能。

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" ); 
}

再后来可以分配给 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();

这篇关于什么用C空函数指针是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆