我想创建一个函数,它执行一组数据上通过参数传递的函数。如何在C中传递函数作为参数?
当前回答
你需要传递一个函数指针。语法有点麻烦,但是一旦你熟悉了它,它真的很强大。
其他回答
将一个函数的地址作为参数传递给另一个函数,如下所示
#include <stdio.h>
void print();
void execute(void());
int main()
{
execute(print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void f()) // receive address of print
{
f();
}
也可以使用函数指针将函数作为参数传递
#include <stdio.h>
void print();
void execute(void (*f)());
int main()
{
execute(&print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void (*f)()) // receive address of print
{
f();
}
宣言
接受函数形参的函数原型如下所示:
void func ( void (*f)(int) );
这说明形参f将是一个指针,指向一个具有void返回类型并接受单个int形参的函数。下面的函数(print)是一个可以作为参数传递给func的函数示例,因为它是正确的类型:
void print ( int x ) {
printf("%d\n", x);
}
函数调用
当调用带有函数形参的函数时,传递的值必须是指向函数的指针。使用函数名(不带括号):
func(print);
调用func,将print函数传递给它。
函数体
与任何形参一样,func现在可以在函数体中使用形参的名称来访问形参的值。假设func将应用它传递给数字0-4的函数。首先,考虑直接调用print的循环会是什么样子:
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
print(ctr);
}
因为func的形参声明说f是指向目标函数的指针的名称,我们首先回想一下,如果f是一个指针,那么*f就是f指向的对象(即在这种情况下的函数打印)。因此,只需将上面循环中每次出现的print替换为*f:
void func ( void (*f)(int) ) {
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
(*f)(ctr);
}
}
源
函数可以作为函数指针“传递”,根据ISO C11 6.7.6.3p8:“将形参声明为“函数返回类型”应调整为“指向函数返回类型的指针”,如6.3.2.1。”。例如,这个:
void foo(int bar(int, int));
等价于:
void foo(int (*bar)(int, int));
这个问题已经有了定义函数指针的答案,但是它们可能会变得非常混乱,特别是如果你要在应用程序中传递它们。为了避免这种不愉快,我建议您将函数指针的类型定义为更可读的类型。为例。
typedef void (*functiontype)();
声明一个返回void且不接受参数的函数。要创建指向该类型的函数指针,您现在可以执行以下操作:
void dosomething() { }
functiontype func = &dosomething;
func();
对于返回int型并接受char型的函数,您可以这样做
typedef int (*functiontype2)(char);
然后使用它
int dosomethingwithchar(char a) { return 1; }
functiontype2 func2 = &dosomethingwithchar
int result = func2('a');
有一些库可以帮助您将函数指针转换为良好的可读类型。boost函数库是伟大的,是非常值得的努力!
boost::function<int (char a)> functiontype2;
比上面的好多了。
typedef int function();
function *g(function *f)
{
f();
return f;
}
int main(void)
{
function f;
function *fn = g(f);
fn();
}
int f() { return 0; }
推荐文章
- PHP中的三个点(…)是什么意思?
- 在Bash中传递带有空格的字符串作为函数参数
- C多行宏:do/while(0) vs作用域块
- 关键字使用virtual+override vs. new
- 在R函数中指定可选参数的“正确”方法
- time_t最终的类型定义是什么?
- 我需要显式处理负数或零时,总和平方数字?
- 函数名周围的括号是什么意思?
- 用C语言创建自己的头文件
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- main()中的Return语句vs exit()
- 局部函数vs Lambda c# 7.0
- 如果不是内存地址,C指针到底是什么?
- 在numpy数组中提取特定列
- 如何从一个Java方法返回2个值?