以下是fromthis excellent SO answer on function pointers;给定一个类似于C的函数指针:
typedef void (*Callback)(int argument);
Callback my_callback = 0;
如何检查回调函数指针是否分配了非空值?
my_callback == 0
或&my_callback == 0
? 最佳答案
您应该检查my_callback == 0
,因为它是一个函数指针。
此外,您正在考虑的第二个选择是:
&my_callback == 0
是地址,编译器甚至会警告您:
warning: the comparison will always evaluate as ‘false’ for the address of ‘my_callback’ will never be NULL [-Waddress]
if(&my_callback == 0)
^
Is NULL always zero in C?