我遇到了在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中省略enum Zahlen时,一切正常。但我需要在我的struct s_action中使用这个枚举。
我如何定义和初始化这个正确的?
答案 0 :(得分:2)
actions
中的字段struct s_testschritt
是一个灵活的数组成员。您无法为其分配数组(或指向数组的指针)。
你想要的是将这个成员声明为指针。然后用数组myactions
初始化它,它将衰减为指向第一个元素的指针。
struct s_testschritt{
int actioncount;
struct s_action *actions;
};
struct s_action myactions[20];
struct s_testschritt aTestschritt = {
.actioncount = 20,
.actions = myactions
};