简体   繁体   English

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

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

I have this struct:我有这个结构:

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

where I need an array of nodes in my node....我的节点中需要一个节点数组....

but in the header file is not recognized: it tell me `unknown type name 'node_t'但在 header 文件中无法识别:它告诉我`未知类型名称'node_t'

how can I solve this?我该如何解决这个问题?

thanks谢谢

In this typedef declaration在这个 typedef 声明中

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

the name node_t within the structure definition is an undeclared name.结构定义中的名称node_t是未声明的名称。 So the compiler will issue an error.所以编译器会报错。

You need to write for example你需要写例如

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

Or you could write或者你可以写

typedef struct node_t node_t;

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

Or even like甚至喜欢

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 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

So I would write like this:所以我会这样写:

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