我的问题非常接近这个问题: How do you declare a const array of function pointers?
我在我的包含文件中成功创建了静态常量函数指针数组。
void fun1( void* );
void fun2( void* );
typedef void ( *funPointer )( void* );
funPointer myFunPointer[2] = { &fun1, &fun2 };
现在我发现了以下内容:我的编译器 (gcc 4.6.3) 在我
(1) 编译包含此 header 的不同 *.o 文件,然后将它们链接在一起(多重定义)——它有助于在数组声明中使用 static 关键字(编辑:实际上函数必须声明为静态)。
(2) 编译包含文件头且不设置数组常量的文件。 (myFunPointer 已声明但未使用)
static const myFunPointer[2] ....
捕获错误/警告。
现在的问题是:我可以解释之前的行为,因为 static 使用“预定义”内存地址,并且函数的多个声明将合并到该地址。这个解释正确吗?如何解释没有const 声明的警告?编译器是否能够自动删除文件中不需要的部分...?
最佳答案
在你的头文件中...
void fun1( void* );
void fun2( void* );
typedef void ( *funPointer )( void* );
extern funPointer myFunPointer[2];
并且在您的一个文件中...
funPointer myFunPointer[2] = { fun1, fun2 };
关于c - 静态常量函数指针 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16199174/