簡體   English   中英

struct node和struct node *之間的'->'有什么區別?

[英]What is the difference in '->' between struct node and struct node*?

我是指針的新手,這里有用於合並鏈表的代碼。 在這里,它已將虛擬節點聲明為struct node dummy; 並且虛擬節點的下一個節點為NULL,因此我們使用dummy.next = NULL;進行設置dummy.next = NULL;

/* Link list node */
struct node
{
    int data;
    struct node* next;
};
struct node* SortedMerge(struct node* a, struct node* b) 
{
   /* a dummy first node to hang the result on */   
   struct node dummy;      

   /* tail points to the last result node  */
   struct node* tail = &dummy;  

   /* so tail->next is the place to add new nodes 
     to the result. */
   dummy.next = NULL;
//Code continues...
}

我知道,如果它是struct node *dummy; ,我可以使用它struct node *dummy; 但是我們不能在這里使用它,因為它不是指針節點。 所以我的問題是為什么dummy->next = NULL在這里不起作用? struct node和struct node *有什么區別?

(*a).b a -> b(*a).b簡寫。

如果a不是指針,則*a無效,並且a- a -> b也不有效。

dummy不是指向結構的指針。 它是結構變量本身。 僅當它是指向結構的指針時,才可以使用運算符->取消引用結構的屬性。

如果您使用struct變量,則. 是解決的方法,對於dummy來說,情況就是如此。

我知道,如果它是struct node *dummy; ,我可以使用它struct node *dummy;

如果用“ it”表示struct node dummy; 那么答案是否定的。 您不能以與指向node的指針相同的方式使用指向node的指針。

所以我的問題是為什么dummy->next = NULL在這里不起作用?

因為dummynode ,而不是指針,所以運算符->如果是指針。 表達式dummy->next具有與(*dummy).next相同的語義。

所以我的問題是為什么dummy-> next = NULL在這里不起作用? struct node和struct node *有什么區別?

聲明為該struct node dummy;

dummy->next=NULL不起作用,因為dummy不是指向struct的指針。

如果您這樣寫-

struct node A;  // then A is a struct variable which can access struct members  using '.' operator

和這個 -

struct node* B; // then B is a pointer to struct node which can access struct member using '->`  or like this (*B).data=something.

暫無
暫無

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

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