source code
#include <stdio.h>
typedef int TypedefTestFunc(int);
TypedefTestFunc typedefTestFunc;
int main() {
printf("Hello, World : %d\n", typedefTestFunc(3));
return 0;
}
int typedefTestFunc(int val){
return val;
}
原本去掉typedef int TypedefTestFunc(int);和TypedefTestFunc typedefTestFunc;这段代码是无法运行的,因为printf函数中typedefTestFunc是未定义的。
现在代码是正常编译运行的。猜测应该是 typedef定义了一个函数类型TypedefTestFunc,然后该函数类型定义了一个typedefTestFunc函数定义,而后边的
int typedefTestFunc(int val){
return val;
}
则是该函数定义的具体实现,由于typedefTestFunc在printf函数之前进行了定义,所以该段代码可以正常编译通过。
这段代码的有趣之处在于可以对typedefTestFunc当做一个TypedefTestFunc类型的变量在函数间进行传递。这个用法在《Unix高级环境编程》中的P105的文件遍历程序实例中使用。另外这段代码也是来源于这里,挺有意思,做一下记录。