结构中的变量不明确 - 双链表
Unclear variables in a struct - Doubly linked list
我正在分析我要在其上构建双向链表的这段代码:
struct dplist_node {
dplist_node_t * prev, * next;
element_t element;
};
struct dplist {
dplist_node_t * head;
// more fields will be added later
};
在包含的头文件中,我发现了以下几行:
typedef int element_t;
typedef struct dplist dplist_t;
typedef struct dplist_node dplist_node_t;
我从这里收集到的是 dplist_node_t 是结构 dplist_node 的类型定义,但仍然有指向 dplist_node_t(因此 dplist_node)的指针定义在两个结构。
我发现所有这些都非常令人困惑,似乎无法弄清楚什么是哪个结构的元素或哪个指针指向哪里。谁能帮我理解一下?
其实这个等价声明更容易理解:
struct dplist_node {
struct dplist_node * prev, * next;
element_t element;
};
struct dplist {
struct dplist_node * head;
// more fields will be added later
};
typedef
在这种情况下只会增加混乱。
有:
typedef struct dplist dplist_t;
你可以写dplist_t
而不是struct dplist
,或者dplist_t*
而不是struct dplist*
。
插图
struct dplist_node {
struct dplist_node * prev, * next;
element_t element;
};
这里很明显prev
和next
是指向struct dplist_node
本身的指针。
原始声明:
struct dplist_node {
dplist_node_t * prev, * next;
element_t element;
};
当我们看到这个时,我们不知道 dplist_node_t
是什么,除非我们查看头文件,其中 dplist_node_t
被定义为 struct dplist
的等价物。所以 IMO 不太清楚。
我正在分析我要在其上构建双向链表的这段代码:
struct dplist_node {
dplist_node_t * prev, * next;
element_t element;
};
struct dplist {
dplist_node_t * head;
// more fields will be added later
};
在包含的头文件中,我发现了以下几行:
typedef int element_t;
typedef struct dplist dplist_t;
typedef struct dplist_node dplist_node_t;
我从这里收集到的是 dplist_node_t 是结构 dplist_node 的类型定义,但仍然有指向 dplist_node_t(因此 dplist_node)的指针定义在两个结构。
我发现所有这些都非常令人困惑,似乎无法弄清楚什么是哪个结构的元素或哪个指针指向哪里。谁能帮我理解一下?
其实这个等价声明更容易理解:
struct dplist_node {
struct dplist_node * prev, * next;
element_t element;
};
struct dplist {
struct dplist_node * head;
// more fields will be added later
};
typedef
在这种情况下只会增加混乱。
有:
typedef struct dplist dplist_t;
你可以写dplist_t
而不是struct dplist
,或者dplist_t*
而不是struct dplist*
。
插图
struct dplist_node {
struct dplist_node * prev, * next;
element_t element;
};
这里很明显prev
和next
是指向struct dplist_node
本身的指针。
原始声明:
struct dplist_node {
dplist_node_t * prev, * next;
element_t element;
};
当我们看到这个时,我们不知道 dplist_node_t
是什么,除非我们查看头文件,其中 dplist_node_t
被定义为 struct dplist
的等价物。所以 IMO 不太清楚。