【问题标题】:What's -> and . in linkedlists? [duplicate]什么是 -> 和 .在链表中? [复制]
【发布时间】:2020-07-18 23:35:54
【问题描述】:

我正在使用 C 中的链表。有人可以解释一下的功能和目的吗:

t->next;

t.next;

【问题讨论】:

  • 你知道什么是指针吗?如果是:a->b(*a).b 的语法糖。所以->首先取消引用一个指针,而.本身没有。如果t 是一个结构或类(MyClass t),t.next 就有意义,如果t 是一个指向结构/类(MyClass* t)的指针t->next 就有意义。如果你不知道什么是指针:请先了解它们,否则所有的解释都对你没有意义。
  • C 教程和书籍可能是一个不错的起点:a_struct_pointer->member vs a_struct.member
  • t 是什么?两个片段都采用不同的定义...

标签: c


【解决方案1】:

要访问structunion 的成员,请使用.。要取消引用指向 structunion 的指针并访问成员 ->,如下面的示例所示

struct node
{
    struct node *next;
};


void foo()
{
    struct node n;
    struct node *np = &n;
     
    // accessing member of the struct 
    n.next = &n;
    // accessing member of the struct via the pointer
    np -> next = &n;

    // accessing member of the struct via the derefenced pointer
    (*np).next = &n;

    (&n) -> next = &n;
}

【讨论】:

    猜你喜欢
    • 2014-07-16
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 2011-07-10
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多