簡體   English   中英

使用指針訪問struct內部的struct

[英]Access struct inside struct with pointers

我有2個結構相互關聯。 這些形成了一個鏈表。

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-> item-> text ,還是必須執行(* list)。(* item).text 或者這根本不可能?

您可以執行您想要的操作,除非您對第二個表單使用了錯誤的語法。 以下是等效的:

list->item->text

和:

(*(*list).item).text

你有什么, (*list).(*item).text ,會導致語法錯誤,因為你必須有一個結構成員. 運營商。

我可以在if語句中執行list->item->text嗎?

是的,因為兩者都是指針。


回想一下a->b(*a).b

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM