【问题标题】:How to initialize array of pointers to functions?如何初始化指向函数的指针数组?
【发布时间】:2011-07-08 17:01:35
【问题描述】:

我有以下代码:

typedef int (*t_Function) (int x);
t_Function Functions[MAX_FUNCTIONS];
int f(int x)
{
    return 0;
}

但我无法正确初始化它。如果我添加以下行:

Functions[0] = f;

然后编译器产生以下错误:

prog.c:217: warning: data definition has no type or storage class
prog.c:217: error: conflicting types for Functions

如何初始化这个函数指针数组?

【问题讨论】:

    标签: c arrays function pointers function-pointers


    【解决方案1】:

    您应该在函数中执行此操作,Functions[0] = f; 可以正常工作,或者使用数组初始值设定项:

    t_Function Functions[MAX_FUNCTIONS] = {f};
    

    为此,f(以及您想要在Functions 中的所有函数)必须已在此定义出现的位置声明。请注意,如果至少有一个以这种方式填充,Functions 的所有其他 MAX_FUNCTIONS-1 元素将自动为 NULL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2015-04-28
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 2012-12-19
      • 1970-01-01
      相关资源
      最近更新 更多