内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
例如:
#include <stdio.h> typedef void (* proto_1)(); typedef void proto_2(); void my_function(int j){ printf("hello from function. I got %d.\n",j); } void call_arg_1(proto_1 arg){ arg(5); } void call_arg_2(proto_2 arg){ arg(5); } void main(){ call_arg_1(&my_function); call_arg_1(my_function); call_arg_2(&my_function); call_arg_2(my_function); }
运行这个程序,我得到以下信息:
> tcc -run try.c hello from function. I got 5. hello from function. I got 5. hello from function. I got 5. hello from function. I got 5.
我的两个问题是:
(* proto)
一个没有定义的?&
)而没有?typedef void (*name1)(void);
和
typedef void(name2)(void);
是不同的:
name1是指向不接受参数而不返回任何参数的函数的指针。
name2是一个不接受参数而不返回任何参数的函数。
您可以通过编译来测试它:
typedef void (*pointer)(void); typedef void (function)(void); void foo(void){} int main() { pointer p; function f; p = foo; //compiles p(); f = foo; //does not compile f(); }