简体   繁体   English

为什么比较空函数指针会导致“无效的操作数到二进制==”

[英]why comparing null function pointer results in “invalid operands to binary ==”

Can somebody please explain to me why the below code got the "invalid operands to binary ==" error? 有人可以向我解释为什么下面的代码将“无效的操作数转换为二进制==“错误吗?

typedef int (*func_t)(int);
#define NO_FUNC ((func_t) 0)
struct {
    const char *name;
    func_t func;
} table[] = { {"func1", NO_FUNC} };

if (table[0] == NO_FUNC) { // invalid operands to binary ==

}

并且您应该在结构中引用正确的成员:

if (table[0].func == NO_FUNC)

table[0] is of an unnamed struct type, and NO_FUNC has type int (*)(int) . table[0]是未命名的struct类型,而NO_FUNC类型是int (*)(int) These two types can't be compared. 这两种类型无法比较。

Instead, you could use: 相反,您可以使用:

if (table[0].func == NO_FUNC)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM