我遇到了在 C 中定义结构的问题。我使用 GCC。 这是代码:
#include <stdio.h>
#include <stdlib.h>
typedef enum Zahlen {
eins =0,
zwei,
drei
}tZahlen;
struct s_action{
tZahlen aZahl;
void *argument;
char name[];
};
struct s_testschritt{
int actioncount;
struct s_action actions[];
};
struct s_action myactions[20];
struct s_testschritt aTestschritt = {
.actioncount = 20,
.actions = &myactions
};
int main(int argc, char *argv[]) {
return 0;
}
这在编译时给我以下错误:
[Error] incompatible types when initializing type 'enum Zahlen' using type 'struct s_action (*)[20]'
当我在 struct s_action 中省略枚举 Zahlen 时,一切正常。但是我的结构 s_action 中需要这个枚举。
我如何正确定义和初始化它?
最佳答案
struct s_testschritt
中的字段actions
是一个灵活的数组成员。您不能为其分配数组(或指向数组的指针)。
您想要的是将此成员声明为指针。然后使用数组 myactions
对其进行初始化,该数组将衰减为指向第一个元素的指针。
struct s_testschritt{
int actioncount;
struct s_action *actions;
};
struct s_action myactions[20];
struct s_testschritt aTestschritt = {
.actioncount = 20,
.actions = myactions
};
关于c - 使用类型 'struct s_action (*) 初始化类型 'enum Zahlen' 时出现类型不兼容错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41966421/