繁体   English   中英

在数组中声明结构类型的元素

[英]declare element in array that is the struct type

我有这个结构:

typedef struct {
    int id;
    node_t * otherNodes;
} node_t;

我的节点中需要一个节点数组....

但在 header 文件中无法识别:它告诉我`未知类型名称'node_t'

我该如何解决这个问题?

谢谢

在这个 typedef 声明中

typedef struct {
    int id;
    node_t * otherNodes;
} node_t;

结构定义中的名称node_t是未声明的名称。 所以编译器会报错。

你需要写例如

typedef struct node_t {
    int id;
    struct node_t * otherNodes;
} node_t;

或者你可以写

typedef struct node_t node_t;

struct node_t {
    int id;
    node_t * otherNodes;
};

甚至喜欢

struct node_t typedef node_t;

struct node_t {
    int id;
    node_t * otherNodes;
};

I referenced defenition of struct list_head from kernel codes: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/include/linux/types.h?h=v5. 10.84#n178

所以我会这样写:

struct node {
    int id;
    struct node * otherNodes;
};

typedef struct node node_t;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM