我一直在研究如何使用 sys/queue.h 中定义的宏来使用单链表,但我有一些疑问。目前我正在尝试使用相同的方法创建一个简单的列表。请注意,下面的程序并不完整,我只放入了我认为相关的部分。
int main() {
SLIST_HEAD(slisthead, entry) head = SLIST_HEAD_INITIALIZER(head);
struct slisthead *headp;
struct entry {
SLIST_ENTRY(entry) entries;
}*n1, *n2, *n3, *np;
/* Upon expanding the macro we would get,
* struct entry {
* struct {
* struct entry *sle_first;
* } entries;
* }*n1, *n2, *np;
*/
我无法理解为什么“sle_first”完全存储在另一个结构中。 SLIST_ENTRY 不能扩展为如下所示吗?
#define SLIST_ENTRY(type) 结构类型* sle_first;
最佳答案
sys/queue.h 还包含双链表和队列,在条目中需要多个指针。
所以我认为 SLIST_ENTRY 通过这种方式扩展,类似于其他类型的条目,例如队列。
关于c - 了解 SLIST -queue.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5168183/