如何定义函数指针和功能的数组没有相同的输入参数定义是什么? [英] How to define an array of function pointers and the functions don't have same input argument definition?

查看:169
本文介绍了如何定义函数指针和功能的数组没有相同的输入参数定义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能定义一个函数指针数组(和功能没有本质上相同的输入参数)作为显示在下面的code?

如果是我必须把函数定义 INT(*处理器)(/ *什么IHAVE放到这里* /);

 结构handler_index {
    为const char *名称;
    INT(*处理器)(/ *什么IHAVE放到这里* /?);
};INT handler0(int类型的,INT B)
{
    的printf(%d个\\ N,A + B);
}INT handler1(int类型的,INT B,INT C)
{
    的printf(%d个\\ N,A + B + C);
}INT handler2(int类型的,INT B,INT C,INT D)
{
    的printf(%d个\\ N,A + B + C + D);
}const的结构handler_index handler_index [] = {
  [0] = {handler0,handler0}
  [1] = {handler1,handler1}
  [2] = {handler2,handler3}
};


解决方案

只要把什么:

  INT(*处理器)();

表示该功能有参数的指定(而非变量)的数量和类型。

这是返回的任何功能的 INT 和一个固定的参数可变数目可分配给处理程序

Is it possible to define an array of function pointers (and the functions don't have se same input argument ) as indicating in the following code ?

If yes what I have to put in the function definition int (*handler)(/*what Ihave to put here ?*/);

struct handler_index {
    const char *name;
    int (*handler)(/*what Ihave to put here ?*/);
};

int handler0 (int a, int b)
{
    printf("%d\n",a+b);
}

int handler1 (int a, int b, int c)
{
    printf("%d\n",a+b+c);
}

int handler2 (int a, int b, int c, int d)
{
    printf("%d\n",a+b+c+d);
}

const struct handler_index handler_index[] = {
  [0] = {"handler0", handler0},
  [1] = {"handler1", handler1},
  [2] = {"handler2", handler3},
};

解决方案

Just put nothing:

int (*handler)();

it means the function has an unspecified (but non-variable) number and types of parameters.

Any function that returns an int and with a fixed variable number of parameters can be assigned to handler.

这篇关于如何定义函数指针和功能的数组没有相同的输入参数定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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