我想创建一个函数,该函数对一组数据执行通过参数传递的函数。在 C 中如何将函数作为参数传递?

有帮助吗?

解决方案

宣言

采用函数参数的函数原型如下所示:

void func ( void (*f)(int) );

这表明参数 f 将是一个指向函数的指针,该函数具有 void 返回类型并且需要一个 int 范围。以下函数(print) 是一个可以传递给的函数示例 func 作为参数,因为它是正确的类型:

void print ( int x ) {
  printf("%d\n", x);
}

函数调用

当使用函数参数调用函数时,传递的值必须是指向函数的指针。为此使用函数名称(不带括号):

func(print);

会打电话 func, ,将打印函数传递给它。

函数体

与任何参数一样,func 现在可以在函数体中使用参数的名称来访问参数的值。假设 func 将应用传递给数字 0-4 的函数。首先考虑一下直接调用 print 的循环是什么样子的:

for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
  print(ctr);
}

自从 func的参数声明表示 f 是指向所需函数的指针的名称,我们首先回想一下,如果 f 那么是一个指针 *f 是这样的事情 f 指向(即功能 print 在这种情况下)。因此,只需将上面循环中出现的每个 print 替换为 *f:

void func ( void (*f)(int) ) {
  for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
    (*f)(ctr);
  }
}

http://math.hws.edu/bridgeman/courses/331/f05/handouts/c-c++-notes.html

其他提示

这个问题已经有了定义函数指针的答案,但是它们可能会变得非常混乱,特别是如果您要在应用程序中传递它们的话。为了避免这种不愉快的情况,我建议您将函数指针键入更具可读性的内容。例如。

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::function<int (char a)> functiontype2;

比上面的好多了。

从 C++11 开始,您可以使用 功能库 以简洁和通用的方式做到这一点。语法是,例如,

std::function<bool (int)>

在哪里 bool 此处是第一个参数为 type 的单参数函数的返回类型 int.

我在下面提供了一个示例程序:

// g++ test.cpp --std=c++11
#include <functional>

double Combiner(double a, double b, std::function<double (double,double)> func){
  return func(a,b);
}

double Add(double a, double b){
  return a+b;
}

double Mult(double a, double b){
  return a*b;
}

int main(){
  Combiner(12,13,Add);
  Combiner(12,13,Mult);
}

但有时,使用模板函数更方便:

// g++ test.cpp --std=c++11

template<class T>
double Combiner(double a, double b, T func){
  return func(a,b);
}

double Add(double a, double b){
  return a+b;
}

double Mult(double a, double b){
  return a*b;
}

int main(){
  Combiner(12,13,Add);
  Combiner(12,13,Mult);
}

经过 一个函数的地址作为另一个函数的参数 如下所示

#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();
}

你需要通过一个 函数指针. 。语法有点麻烦,但是一旦熟悉了它就非常强大。

函数可以作为函数指针“传递”,如 6.7.6.3p8 所示:”将参数声明为“函数返回类型”应调整为“指向函数返回类型的指针”,如 6.3.2.1 中所示。 ”。例如,这个:

void foo(int bar(int, int));

等价于:

void foo(int (*bar)(int, int));

它并不是真正的函数,而是一段本地化的代码。当然它不传递代码,只传递结果。如果传递给事件调度程序以便稍后运行,它将不起作用(因为结果是现在计算的,而不是在事件发生时计算的)。但如果您只想这样做,它确实会将您的代码本地化到一个地方。

#include <stdio.h>

int IncMultInt(int a, int b)
{
    a++;
    return a * b;
}

int main(int argc, char *argv[])

{
    int a = 5;
    int b = 7;

    printf("%d * %d = %d\n", a, b, IncMultInt(a, b));

    b = 9;

    // Create some local code with it's own local variable
    printf("%d * %d = %d\n", a, b,  ( { int _a = a+1; _a * b; } ) );

    return 0;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top