当我阅读其他人的代码时,我总是有点困惑,这些代码为带有参数的函数指针提供了typedefs。我记得,当我试图理解不久前用C语言编写的数值算法时,我花了一段时间才得到这样的定义。那么,关于如何为函数指针编写良好的typedef (Do's和Do's),以及它们为什么有用以及如何理解其他人的工作,您能分享一下您的技巧和想法吗?谢谢!
当前回答
A function pointer is like any other pointer, but it points to the address of a function instead of the address of data (on heap or stack). Like any pointer, it needs to be typed correctly. Functions are defined by their return value and the types of parameters they accept. So in order to fully describe a function, you must include its return value and the type of each parameter is accepts. When you typedef such a definition, you give it a 'friendly name' which makes it easier to create and reference pointers using that definition.
例如,假设你有一个函数:
float doMultiplication (float num1, float num2 ) {
return num1 * num2; }
然后是下面的类型定义:
typedef float(*pt2Func)(float, float);
可以用来指向这个doMulitplication函数。它只是定义一个指向函数的指针,该函数返回一个浮点数,并接受两个参数,每个参数都是float类型。这个定义有一个友好的名字pt2Func。请注意,pt2Func可以指向返回一个浮点数并接受2个浮点数的任何函数。
所以你可以创建一个指针指向doMultiplication函数,如下所示:
pt2Func *myFnPtr = &doMultiplication;
你可以使用这个指针调用函数,如下所示:
float result = (*myFnPtr)(2.0, 5.1);
这是一个很好的阅读:http://www.newty.de/fpt/index.html
其他回答
int add(int a, int b)
{
return (a+b);
}
int minus(int a, int b)
{
return (a-b);
}
typedef int (*math_func)(int, int); //declaration of function pointer
int main()
{
math_func addition = add; //typedef assigns a new variable i.e. "addition" to original function "add"
math_func substract = minus; //typedef assigns a new variable i.e. "substract" to original function "minus"
int c = addition(11, 11); //calling function via new variable
printf("%d\n",c);
c = substract(11, 5); //calling function via new variable
printf("%d",c);
return 0;
}
它的输出是:
22
6
注意,声明这两个函数时使用了相同的math_func定义器。
与typedef相同的方法可以用于extern struct。(在其他文件中使用struct)
A function pointer is like any other pointer, but it points to the address of a function instead of the address of data (on heap or stack). Like any pointer, it needs to be typed correctly. Functions are defined by their return value and the types of parameters they accept. So in order to fully describe a function, you must include its return value and the type of each parameter is accepts. When you typedef such a definition, you give it a 'friendly name' which makes it easier to create and reference pointers using that definition.
例如,假设你有一个函数:
float doMultiplication (float num1, float num2 ) {
return num1 * num2; }
然后是下面的类型定义:
typedef float(*pt2Func)(float, float);
可以用来指向这个doMulitplication函数。它只是定义一个指向函数的指针,该函数返回一个浮点数,并接受两个参数,每个参数都是float类型。这个定义有一个友好的名字pt2Func。请注意,pt2Func可以指向返回一个浮点数并接受2个浮点数的任何函数。
所以你可以创建一个指针指向doMultiplication函数,如下所示:
pt2Func *myFnPtr = &doMultiplication;
你可以使用这个指针调用函数,如下所示:
float result = (*myFnPtr)(2.0, 5.1);
这是一个很好的阅读:http://www.newty.de/fpt/index.html
一个非常简单的方法来理解函数指针的typedef:
int add(int a, int b)
{
return (a+b);
}
typedef int (*add_integer)(int, int); //declaration of function pointer
int main()
{
add_integer addition = add; //typedef assigns a new variable i.e. "addition" to original function "add"
int c = addition(11, 11); //calling function via new variable
printf("%d",c);
return 0;
}
这是我作为练习写的关于函数指针和函数指针数组的最简单的例子。
typedef double (*pf)(double x); /*this defines a type pf */
double f1(double x) { return(x+x);}
double f2(double x) { return(x*x);}
pf pa[] = {f1, f2};
main()
{
pf p;
p = pa[0];
printf("%f\n", p(3.0));
p = pa[1];
printf("%f\n", p(3.0));
}
宏呢?我已经看到,使用类型定义,我不能将一些东西强制转换为函数指针。所以我为它做了这个小宏,它能够做我到目前为止需要的所有事情(不知道它不能用于什么-我认为它基本上是一个类型定义,但有一个可选参数,使它在类型转换中可用):
#define funcptr_t(sym_name) void (*sym_name)(void)
// Declare a function pointer-returning function, and declare a function pointer variable.
funcptr_t (randomFunction(funcptr_t (func_ptr_variable)));
// Cast a variable to a function pointer
(funcptr_t()) some_variable;
PS:我做这个通用用途,不确定它是否可以修改为特定的功能使用。
推荐文章
- C多行宏:do/while(0) vs作用域块
- time_t最终的类型定义是什么?
- 我需要显式处理负数或零时,总和平方数字?
- 函数名周围的括号是什么意思?
- 用C语言创建自己的头文件
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- main()中的Return语句vs exit()
- 如果不是内存地址,C指针到底是什么?
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 保护可执行文件不受逆向工程的影响?
- 从C语言的函数返回一个struct
- C99 'restrict'关键字的实际用法?
- 检查子字符串存在于C中的字符串中
- 从标准输入中捕获字符,而不需要等待按enter键
- 查找当前可执行文件的路径,不包含/proc/self/exe