[英]What is the difference between struct node *head and struct node ** head?
[英]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
在這里不起作用?
因為dummy
是node
,而不是指針,所以運算符->
如果是指針。 表達式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.