问题描述
下面的函数typedef
有什么区别:
What is the difference between the following function typedef
:
typedef void tbl_opt (void *tbl_item, void *tbl_param); // 1
typedef void *tbl_opt (void *tbl_item, void *tbl_param); // 2
typedef void (*tbl_opt) (void *tbl_item, void *tbl_param); // 3
typedef (void *) (*tbl_opt) (void *tbl_item, void *tbl_param); // 4
通常,我使用第三个 &第4个,但我在书上看到另外2个,我不太明白.
Usually, I use the 3rd & 4th, but I saw the other 2 in a book, which I don't understand well.
[更新] 总结:
似乎在case 1或2中定义类型时,使用该类型时,仍然需要声明一个指针变量,并将其初始化为指向另一个实际函数,所以我想这就是case 3 &4 个更受欢迎.
It seems when define type in case 1 or 2, when use that type, still need declare a pointer variable, and initialize it to point to another actual function, so I guess that's why case 3 & 4 are more popular.
推荐答案
3 和 4 是函数指针:
3 and 4 are Function Pointers:
返回 void 或不返回值并接受两个 void* 参数的函数
A Function that returns void or nothing and takes two void* arguments
typedef void tbl_opt (void *tbl_item, void *tbl_param);
返回一个 void* 并接受两个 void * 参数的函数
A function that returns a void* and takes two void * arguments
typedef void *tbl_opt (void *tbl_item, void *tbl_param);
指向一个函数的函数指针,该函数返回 void 或不返回并接受两个 void* 参数
A function pointer to a function that returns void or nothing and takes two void* arguments
typedef void (*tbl_opt) (void *tbl_item, void *tbl_param);
指向返回 void* 并接受两个 void* 参数的函数的函数指针
A Function pointer to a function that returns void* and takes two void* arguments
typedef (void *) (*tbl_opt) (void *tbl_item, void *tbl_param);
这篇关于typedef 在函数上的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!