函数指针是什么,到底怎么用?
#include <stdio.h>
typedef int(*P_FUNC)(int,...);/*redefine function point*/
int fun1(int a,...) /*function*/
{
printf ("hello world:%d\n",a);
return a;
}
void main(void)
{
int tmp;
//int (*p)(int,...); /*way1: define the point of function: p */
P_FUNC p; /*way2: define the point of function : p */
P_FUNC c; /*way2: define the point of function : c */
p=fun1;
c=fun1;
tmp=p(100,200,299,339);
printf("end id %d\n",tmp);
tmp=p(200);
printf("end id %d\n",tmp);
}
int (*p)(int,…):这是结构指针的定义,类型(指针变量)(参数,参数….)。这样的函数指针可以指向类型和参数与之一致的函数。如例子上的函数 int fun1(int a,…)
还可以使用重定向的方法:typedef int(*P_FUNC)(int,…) 这个语句的P_FUNC代表了类型的参数的函数指针。但是没有指定的指针变量,所以还得定义指针变量 如P_FUNC p;,要定义很多函数指针变量的时候。这样更方便
有些函数的参数是(…),这代表了函数可以输入多个参数。但是多输入的参数没被用到。这个作用是为了以后给这个函数扩展参数用的:例如tmp=p(100,200,299,339); 只有第一个参数有用,但是你多加参数也不会报错。