im在C中定义结构时遇到了问题。我与海湾合作委员会合作。这是代码:
#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]'
当我省略结构中的枚举 Zahlen 时s_action一切正常。但是我的结构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
};