我有两个相互关联的结构。这些形成了一个链表。
typedef struct {
char *text;
int count;
} *Item;
typedef struct node {
Item item;
struct node *next;
} *link;
我正在构建一个查找函数来比较Item结构。
link lookup(link head, Item item){
link list;
for(list = head; list != NULL; list = list->next)
if(strcmp(list->item->text, item->text) == 0)
return list;
return NULL;
}
更具体地说,我可以在if语句中执行 list-&gt; item-&gt; text ,还是必须执行(* list)。(* item).text < / strong>?或者这根本不可能?
答案 0 :(得分:2)
您可以执行您想要的操作,除非您对第二种形式使用了错误的语法。以下是等效的:
list->item->text
和
(*(*list).item).text
您拥有的内容(*list).(*item).text
会导致语法错误,因为您必须在.
运算符后面有一个结构成员。
答案 1 :(得分:1)
我可以在if语句上做
list->item->text
吗?
是的,因为两者都是指针。
回想一下,a->b
与(*a).b
相同。