C中嵌套结构中的访问项

我尝试在另一个结构中访问“下一个”,但失败了,尽管我尝试了很多方法。

这是嵌套结构:

struct list_head { struct list_head *next, *prev; }; typedef struct { char *key; char *value; struct list_head list; }dict_entry; 

我想访问“下一步”。 我初始化了一个新的dict_entry。

 dict_entry *d; while(d->list->next!=NULL){} 

但这是错的。 谁能给我一个访问“下一步”的方法? 注意:我无法改变结构。

list没有被声明为指针,所以你不使用->运算符来获取它的成员,你使用了. 运营商:

 while (d->list.next != NULL) { } 

一个不同的修复:

 typedef struct { char *key; char *value; struct list_head *list; }dict_entry; 

这样,您尝试引用next原始代码就会编译。

在您的定义中,您将list定义为对象,如下所示

 typedef struct { char *key; char *value; struct list_head list; // This is an object }dict_entry; 

因此,您将通过next取消引用. 运算符为d->list.next 。 第一级d->list引用,即d->list需要->运算符,因为d被定义为指针。 next ,因为list是一个对象而不是指针,所以你必须使用它. 运营商。