c - 函数指针数组错误: Initialization from incompatible pointer type

标签 c arrays pointers

我想创建一个函数指针数组,其中包含指向函数的指针数组。但我不断在 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/

相关文章:

c - 两个进程共享内存中的结构体数组

arrays - 使用滑动窗口查看 Julia 数组

c - 访问不带括号的函数指针

c - 在 c 中用 ctime 格式化 Unix 时间戳

c++ - 结构/类成员的显式初始化

c - 关于Linux fork的问题

php - 值未通过 ajax 将 session 数组更新为 json 数组

php - 在提交到数据库之前从字符串中提取信息

C指针问题

c - 指向未初始化变量的指针