如何使用函数指针数组? [英] How can I use an array of function pointers?

查看:120
本文介绍了如何使用函数指针数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何使用函数指针数组用C?

How should I use array of function pointers in C?

我该如何初始化呢?

推荐答案

您有一个很好的例子的在这里(函数指针数组),与语法的详细

You have a good example here (Array of Function pointers), with the syntax detailed.

int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);

int (*p[4]) (int x, int y);

int main(void)
{
  int result;
  int i, j, op;

  p[0] = sum; /* address of sum() */
  p[1] = subtract; /* address of subtract() */
  p[2] = mul; /* address of mul() */
  p[3] = div; /* address of div() */
[...]

要调用这些函数指针之一:

To call one of those function pointers:

result = (*p[op]) (i, j); // op being the index of one of the four functions

这篇关于如何使用函数指针数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆