liv*_*hak 1 c pointers linked-list data-structures
我想声明一个自引用结构,如下所示
typedef struct
{
entry_t *entry;
node_t *next;
}node_t;
Run Code Online (Sandbox Code Playgroud)
而不是链接列表的下面
struct node
{
entry_t *entry;
struct node *next;
}*head;
Run Code Online (Sandbox Code Playgroud)
这在C中有用吗?如果不是为什么不呢?
它不起作用,因为符号/名称node_t
在声明中未知next
:
typedef struct
{
entry_t *entry;
node_t *next; /* <-- Error: unknown type */
} node_t;
Run Code Online (Sandbox Code Playgroud)
您的结构在其声明中需要一个名称才能成为"自我引用".但是,您可以保留typedef:
typedef struct node
{
entry_t *entry;
struct node *next; /* <-- type struct node is known */
} node_t;
Run Code Online (Sandbox Code Playgroud)
现在您可以使用struct node
或node_t
创建新节点.