c - 使用类型 'struct s_action (*) 初始化类型 'enum Zahlen' 时出现类型不兼容错误

标签 c gcc struct enums

我遇到了在 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/

相关文章:

c - 当有空格时如何退出scanf循环

c++ - DistCC 和 CMake - 在运行 make 时在本地和分布式构建之间进行选择

c - 链接 C 中的库,未识别的引用

c - linux 是否在较低的堆栈端以下提供了保证不可访问的内存区域?

c++ - 如何使用 valgrind 3.7.0 打印内存泄漏根本原因的行号?

c - 从 Windows 中的文件或命令进行管道传输/重定向时出错

linux - 无法编译GCC,但没有给出具体错误

swift - 在 Swift 中,如何在结构中将可选枚举初始化为 nil?

python - Python 中更节省内存的结构表示?

go - 使用反射迭代结构的结构成员并在其上调用方法