我想创建一个函数指针数组,其中包含指向函数的指针数组。但我不断在 fun_ptr fun_ptr_arr[3] = {fun1, fun2, fun3};
初始化程序中收到不兼容指针类型错误的初始化。
我尝试了 typedef void (*fun_ptr) (myStruct**,char**);
但错误仍然存在,我不确定出了什么问题。
struct myStruct
{
char *value;
struct myStruct *next;
}
typedef void (*fun_ptr) (myStruct**,char*[]);
void fun1 (myStruct **theStruct, const char **arguments) {};
void fun2 (myStruct **theStruct, const char **arguments) {};
void fun3 (myStruct **theStruct, const char **arguments) {};
void main()
{
fun_ptr fun_ptr_arr[3] = {fun1, fun2, fun3};
}
对于每个函数,我的错误
test.c:285:69: note: (near initialization for ‘fun_ptr_arr[0]’)
test.c:285:69: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
fun_ptr fun_ptr_arr[3] = {fun1, fun2, fun3};
^~~~~~~~
最佳答案
您的原型(prototype)是typedef void (*fun_ptr) (myStruct**,char*[]);
您的函数是:void fun1 (myStruct **, const char **)
注意额外的const
这些是不同的类型,因此编译器会提示。
关于c - 函数指针数组错误: Initialization from incompatible pointer type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56607288/