问:“typedef int (*funcptr)();”是什么意思?
答:它定义了一个类型funcptr,表示指向返回值为int型,参数未指明的函数指针,它可以被用来声明一个或多个函数指针,例如:
funcptr func1, func2;
这个声明等价于下面写法的函数指针声明
int (*func1)(), (*func2)();
验证程序如下:
#include <stdio.h>
typedef int (*func_ptr)();
int test1(void){
printf("test1 is called!\n");
return 1;
}
int test2(void){
printf("test2 is called!\n");
return 1;
}
int main(){
func_ptr func1, func2;
int (*fptr1)(), (*fptr2)();
func1=test1;
func2=test2;
fptr1=test1;
fptr2=test2;
func1();
printf("======================\n");
func2();
printf("======================\n");
fptr1();
printf("======================\n");
fptr2();
return 1;
}
打印结果如下:
zzl@ubuntu:~/test/test_function_point$ ./test_function_point
test1 is called!
======================
test2 is called!
======================
test1 is called!
======================
test2 is called!