函数指针作为参数

发布于 2024-08-12 10:00:03 字数 70 浏览 3 评论 0原文

是否可以将函数指针作为参数传递给 C 中的函数?

如果是这样,我将如何声明和定义一个以函数指针作为参数的函数?

Is it possible to pass a function pointer as an argument to a function in C?

If so, how would I declare and define a function which takes a function pointer as an argument?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

↙温凉少女 2024-08-19 10:00:03

确实。

void f(void (*a)()) {
    a();
}

void test() {
    printf("hello world\n");
}

int main() {
     f(&test);
     return 0;
}

Definitely.

void f(void (*a)()) {
    a();
}

void test() {
    printf("hello world\n");
}

int main() {
     f(&test);
     return 0;
}
早乙女 2024-08-19 10:00:03

假设您有一个函数:

int func(int a, float b);

那么指向它的指针将是:

int (*func_pointer)(int, float);

然后,您可以像这样使用它:

  func_pointer = func;
  (*func_pointer)(1, 1.0);

  /*below also works*/
  func_pointer(1, 1.0);

为了避免每次需要时都指定完整的指针类型,您可以 typedef 它:

typedef int (*FUNC_PTR)(int, float);

并且然后像使用任何其他类型一样使用它:

void executor(FUNC_PTR func)
{ 
   func(1, 1.0);
}

int silly_func(int a, float b)
{ 
  //do some stuff
}

main()
{
  FUNC_PTR ptr;
  ptr = silly_func;
  executor(ptr); 
  /* this should also wotk */
  executor(silly_func)
}

我建议查看世界著名的 C 常见问题解答

Let's say that you have a function:

int func(int a, float b);

So the pointer to it will be:

int (*func_pointer)(int, float);

Then, you could use it like this:

  func_pointer = func;
  (*func_pointer)(1, 1.0);

  /*below also works*/
  func_pointer(1, 1.0);

To avoid specifying the full pointer type every time you need it you coud typedef it:

typedef int (*FUNC_PTR)(int, float);

And then use it like any other type:

void executor(FUNC_PTR func)
{ 
   func(1, 1.0);
}

int silly_func(int a, float b)
{ 
  //do some stuff
}

main()
{
  FUNC_PTR ptr;
  ptr = silly_func;
  executor(ptr); 
  /* this should also wotk */
  executor(silly_func)
}

I suggest looking at the world-famous C faqs.

等风来 2024-08-19 10:00:03

这是一个很好的例子:

int sum(int a, int b)
{
   return a + b;
}

int mul(int a, int b)
{
   return a * b;
}

int div(int a, int b)
{
   return a / b;
}

int mathOp(int (*OpType)(int, int), int a, int b)
{
   return OpType(a, b);
}

int main()
{

   printf("%i,%i", mathOp(sum, 10, 12), mathOp(div, 10, 2));
   return 0;
}
输出为:'22, 5'

This is a good example :

int sum(int a, int b)
{
   return a + b;
}

int mul(int a, int b)
{
   return a * b;
}

int div(int a, int b)
{
   return a / b;
}

int mathOp(int (*OpType)(int, int), int a, int b)
{
   return OpType(a, b);
}

int main()
{

   printf("%i,%i", mathOp(sum, 10, 12), mathOp(div, 10, 2));
   return 0;
}
The output is : '22, 5'
¢蛋碎的人ぎ生 2024-08-19 10:00:03

正如其他答案所述,您可以这样做,

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

但是,声明函数指针类型的参数有一种特殊情况:如果参数具有函数类型,它将被转换为指向函数类型的指针,就像数组会转换为参数列表中的指针,因此前者也可以写为

void qsort(void *base, size_t nmemb, size_t size,
           int compar(const void *, const void *));

当然,这仅适用于参数,如在参数列表之外 int compar(const void *, const void *); 会声明一个函数。

As said by other answers, you can do it as in

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

However, there is one special case for declaring an argument of function pointer type: if an argument has the function type, it will be converted to a pointer to the function type, just like arrays are converted to pointers in parameter lists, so the former can also be written as

void qsort(void *base, size_t nmemb, size_t size,
           int compar(const void *, const void *));

Naturally this applies to only parameters, as outside a parameter list int compar(const void *, const void *); would declare a function.

遗弃M 2024-08-19 10:00:03

检查qsort()

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

函数的最后一个参数是函数指针。当您在自己的程序中调用 qsort() 时,执行会通过使用该指针“进入库”并“退回到您自己的代码”。

Check qsort()

void qsort(void *base, size_t nmemb, size_t size,
           int (*compar)(const void *, const void *));

The last argument to the function is a function pointer. When you call qsort() in a program of yours, the execution "goes into the library" and "steps back into your own code" through the use of that pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文