繁体   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