static const函数指针C.

时间:2013-04-24 18:07:18

标签: c arrays function-pointers

我的问题非常接近这个问题: How do you declare a const array of function pointers?

我在我的include文件中成功创建了静态const函数指针数组。

void fun1( void* );
void fun2( void* );
typedef void ( *funPointer )( void* );
funPointer myFunPointer[2] = { &fun1, &fun2 };

现在我发现了以下内容:我的编译器(gcc 4.6.3)在我

时抱怨

(1)编译包含此头文件的不同* .o文件,然后将它们链接在一起(多重定义) - 它有助于在数组的声明中使用static关键字(编辑:实际上函数必须声明为静态)。

(2)编译包含头部的文件,而不是设置数组const。 (myFunPointer声明但未使用)

static const myFunPointer[2] ....

抓住错误/警告。

现在的问题是:我可以解释前一种行为,因为静态使用“预定义”的内存地址,并且函数的几个声明将在地址处合并。这个解释是否正确?如何解释const声明警告的缺失?编译器是否能够自动删除文件中不必要的部分......?

1 个答案:

答案 0 :(得分:6)

在你的标题文件中......

void fun1( void* );
void fun2( void* );
typedef void ( *funPointer )( void* );
extern funPointer myFunPointer[2];

以及来源文件的一个 ...

funPointer myFunPointer[2] = { fun1, fun2 };