gpt4 book ai didi

c - 为什么我不能在 C 中初始化包含函数指针的结构数组?

转载 作者:太空宇宙 更新时间:2023-11-04 02:46:50 25 4
gpt4 key购买 nike

my_functions.h

void * f1 (int * param);
void * f2 (int * param);
void * f3 (int * param);
void b1(int * param);
void b2(int * param);
void b3(int * param);

my_prog.c

#include <my_functions.h>

// Typedef my function pointers
typedef void * (*foo)(int*);
typedef void (*bar)(int*);

// Declare the structure that will contain function pointers
typedef struct _my_struct
{
int tag;
foo f;
bar b;
}my_struct;

// Declare and initialize the array of my_struct
my_struct array[] =
{
{1, f1, b1},
{2, f2, b2},
{3, f3, b3}
};

编译器说:

warning: initialization from incompatible pointer type

我看了:

但是我还是看不到我错过了什么……
对我来说,当我尝试初始化我的数组时,所有类型和函数都是已知的。
初始化不能在函数外完成吗?

[编辑] 我在 Linux 上,使用 GCC 4.9 的嵌入式版本。

最佳答案

好的,所以我的代码在我的 PC 上工作得很好,实际上是我用于嵌入式目标的 arm-gcc 编译器抛出了这个错误,因为它似乎无法识别函数指针的类型而没有显式 Actor :

这里是修复:

my_struct array[] =
{
{1, (foo)f1, (bar)b1},
{2, (foo)f2, (bar)b2},
{3, (foo)f3, (bar)b3}
};

关于c - 为什么我不能在 C 中初始化包含函数指针的结构数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com