我最近在读Linux内核。 我发现在很多情况下,他们使用struct“typedef xxx f(xxx)”,但我不明白它是如何工作的。 (像函数指针?)关于函数指针在C/C++中的问题
这是我的测试代码。
#include<stdio.h>
typedef int Myfunc(int);
typedef int (*point_to_myfunc)(int);
static Myfunc example;
static int example(int a){
printf("example a=%d\n", a);
return 1;
}
static void example2(Myfunc* f){
printf("example2\n");
f(2);
}
static void example3(int (*)(int));
static void example3(int (*point_to_Myfunc)(int)){
printf("example3\n");
point_to_Myfunc(3);
}
int main(){
point_to_myfunc f=&example;
example2(f);
example3(f);
return 0;
}
任何人都可以为我提供一个简短的解释吗? Thx〜
是'typedef int Myfunc(int);'甚至合法吗? – Shoe 2013-03-22 05:46:54
@Jeecy为什么不呢?它有一个非常明显的含义,为什么标准不允许它?如果您想知道typedef是否合法,只需删除“typedef”并查看它是否是合法声明。 'int MyFunc(int);'... yup。 – 2013-03-22 05:56:05