How do you pass a function as a parameter in C?

c function pointers syntax parameters

470906 观看

7回复

I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C?

作者: andrewrk 的来源 发布者: 2019 年 5 月 29 日

回应 (7)


657

决定

Declaration

A prototype for a function which takes a function parameter looks like the following:

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

This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:

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

Function Call

When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:

func(print);

would call func, passing the print function to it.

Function Body

As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:

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

Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:

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

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

作者: Niyaz 发布者: 13.08.2008 02:22

115

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

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;

比上面好多了。

作者: roo 发布者: 13.08.2008 02:34

54

从C ++ 11开始,您可以使用函数库以简洁和通用的方式执行此操作。语法是,例如,

std::function<bool (int)>

这里bool的返回类型是单参数函数,其第一个参数是类型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);
}
作者: Richard 发布者: 20.07.2015 04:24

13

传递函数作为参数传递给另一个函数的地址,如下所示

#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();
}
作者: Yogeesh H T 发布者: 23.11.2015 12:07

3

您需要传递一个函数指针。语法有点麻烦,但是一旦你熟悉它就会非常强大。

作者: saint_groceon 发布者: 13.08.2008 02:18

1

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

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

相当于:

void foo(int (*bar)(int, int));
作者: doppelheathen 发布者: 27.11.2018 03:42

0

它不是真正的功能,但它是一段本地化的代码。当然,它不会仅仅通过代码传递结果。如果传递给事件调度程序以便稍后运行它将无效(因为结果是现在计算的,而不是在事件发生时)。但它确实将您的代码本地化到一个地方,如果这就是您要做的全部。

#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;
}
作者: Walter 发布者: 15.03.2019 02:46
32x32