今天,突然大发奇想,深入的思考了函数指针,对其总结如下:望对初学者有所帮助
//声明函数指针的两种方式
int (*pfunction1)(int,double); //声明返回int类型,参数为int和double的函数指针,函数指针是有类型的
typedef int (*PFUNCTION1)(int,double); //这样一来PFUNCTION1就成为了某种函数指针(参数为int和double,返回int)的类型的别名,因此可以用它来声明变量,如下面一句
PFUNCTION1 function1;
//声明函数指针数组两种方式
int (*pfunction2[3])(int*,int (*p)(float,char));
typedef int (*PFUNCTION2[3])(int* ,double); //定义函数指针数组类型PFUNCTION2
PFUNCTION2 function2;
PFUNCTION2 (*ppfunc)[4]; //定义一个指向数组的指针,。数组中的每个元素都是含有3个函数指针的数组
typedef double (*pff[3])(int); //定义函数指针数组类型pff
pff (*param2)[9]; //定义一个数组指针,数组指针指向的鼠族元素是数组指针
//声明指向数组的指针,把数组声明中的变量名换成(*parray)即可
int (*pArray)[10];
typedef int (*PARRAY)[10]; //PARRAY成为一种数据类型,即指向含有10个整数的数组的指针类型,用它来声明该类性的变量,如下面一句
PARRAY array1;
//声明指向函数指针数组的指针,两种方式,将普通函数指针数组声明中的数组名换成(*pFunctionArray)即可
//int (*p[3])(int*,double); //函数指针数组声明,用(*pFunctionArray)替换p即可,如下
int (*(*pFunctionArray)[3])(int*,int (*)(int,char));
typedef int (*(*PFUNCTIONARRAY)[3])(int*,double); //同样可以将其声明为一种数据类型,然后用它来声明该类型的变量,如下面一句
PFUNCTIONARRAY functionarray1;
//指向数组的指针作为参数和返回值的函数声明
int fun(int (*parr)[]); //指向数组的指针作为参数
typedef int (*PARR)[10]; //使用typedef时的声明方式
PARR arr1;
typedef int FUNC(PARR arr1);
FUNC fun1;
int (*fun2(int (*)[10])) [10]; //不使用typedef时指向数组的指针作为参数和返回值的函数声明
typedef PARR FUNCTION(PARR); //使用typedef时的指向数组的指针作为参数和返回值的函数声明
FUNCTION f1;
//返回和参数都是函数指针的函数指针声明
int (*(*pfunc10)(int (*p)(int*,double),int))(void (*p)(int*,double,float),int,char); //不使用typedef时的声明
typedef double (*pff3)(int ,double);
pff3 (*pfunc3)(int); //返回函数指针的函数指针的声明
typedef int (*PF[])(int*,double);
typedef int (*P)(int*,double);
P func10(PF); //参数是函数指针数组,返回值是函数指针,这就避免了c语言中不能返回数组的缺陷
P func9(P); //参数是函数指针,返回值也是函数指针
typedef double (*pff2)(int); // 使用typedef时的返回函数指针的函数指针的声明
pff2 (*paraminfo)(int);
double (*(*pfunction)(int,double)) (int); //不使用typedef时的返回函数指针的函数指针的声明
//指向函数指针数组的指针作为参数和返回值的函数声明
void (*b[10]) (void (*)()); // 可以将b换成(*pb),这样声明的就是指向函数指针数组的指针,如下面一句所示
void (*(*pb)[10]) (void (*)()); //pb是一个指向函数指针数组的指针
typedef void (*(*PFUNCTIONPOINTERARRAY[3]))(int (*)(int,float));
PFUNCTIONPOINTERARRAY pfuncpointerarray; //声明一个指向函数指针数组的指针
//int (*p[3])(int*,double); //函数指针数组
int (*func1(int (*(*[10]))(int*,double)))(int*,double); //指向函数数组的指针作为参数,返回函数指针
// int (*func1(int (*(*[10]))(int*,double)))(int*,double)[2];
int a(int (*[])(int*) );
//参数是函数指针数组,返回的是函数指针
int (*(*pfunc9)(int (*p[])(int*,double),int))(int (*p[])(int*,double),int); //实际还是返回了一个函数指针(参数是int (*p[])(int*,double)和int,返回值是int)
//参数是指向函数数组的指针,返回值也是指向函数数组的指针
所用的编译器是vc6,以上声明均都是经过测验的!恐怕大多数的人看到上面的声明都会晕
![](https://i-blog.csdnimg.cn/blog_migrate/76db37a1497f219725804e8b5be851d1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/76db37a1497f219725804e8b5be851d1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/76db37a1497f219725804e8b5be851d1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/76db37a1497f219725804e8b5be851d1.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/865cdff654e1f8c6552c2f0b131352d7.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/865cdff654e1f8c6552c2f0b131352d7.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/865cdff654e1f8c6552c2f0b131352d7.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/865cdff654e1f8c6552c2f0b131352d7.gif)