如何初始化函数指针数组? [英] How to initialize array of pointers to functions?

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

问题描述

我有以下code:

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

但我不能正确初始化。如果我添加以下行:

But I cannot initialize it properly. If I add following line:

Functions[0] = f;

然后编译器会生成以下错误:

then compiler generates following error:

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

如何这个数组指针的初始化功能?

How to initialize this array of pointers to functions?

推荐答案

您要么做一个函数,里面其中功能[0] = F; 工作正常,或数组初始化:

You should either do it inside a function, where Functions[0] = f; works fine, or with an array initializer:

t_Function Functions[MAX_FUNCTIONS] = {f};

对于这项工作,˚F(你想要的所有功能在功能)必须已在申报点这个定义出现。需要注意的是函数的所有其他 MAX_FUNCTIONS个-1 元素 NULL 如果自动将它们中的至少一个被填充这样

For this to work, f (and all functions you want in Functions) must have been declared at the point where this definition appears. Note that all other MAX_FUNCTIONS-1 elements of Functions will be NULL automatically if at least one of them is filled this way.

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

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