![](/img/trans.png)
[英]Declare struct type, insert values into array of the type, and print out array
[英]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.