typedef的用法
1、#include <stdio.h></stdio.h>int fun(){
printf("call fun\n");
return 1;
}
typedef int (*funcptr)(); //定义它的用法
int main(){
funcptr ptr=fun;
int i=ptr();
printf("%d\n",i);
return 0;
}
定义了一个函数指针类型funcptr,该指针类型用来指向返回值为int类型,且没有参数(按C++规定,如果按C规定表示对参数没有限制)的函数地址。
2、作为参数传入
/*========Data Define================*/
typedef void (*VP)();
static VP func_table ;
/*========subFunction================*/
void pig(void)
{
printf("This is a pig!\n");
}
void dog(void)
{
printf("This is a dog!\n");
}
void animal(VP p1,VP p2)
{
func_table = p1;
func_table = p2;
}
/*========mainFunction================*/
int main(int argc, char* argv[])
{
animal(pig,dog);
func_table();
func_table();
getchar();
return 0;
}
页:
[1]