c - 在 struct 中使用 struct 关键字

标签 c struct

typedef struct node
{
    int n;
    struct node* next;
}
node;

在上面的代码中,struct node 中有struct node* next。我不明白它的意思。

最佳答案

In the code there is struct node* next inside the struct node. I don't understand the meaning of it.

解释:

节点 是一个通用术语,用于指代 self referential structure .(点击链接了解更多)

按照定义,

A self referential structure is a typical structure whose definition has one or more of its member as a pointer to its own type.

在你的代码中,next 是结构 struct node 的一个成员,它是指向它自己类型的指针,因此你将它视为指向自身的指针结构内部的方式:

struct node* next;

这就是为什么在struct内部使用struct关键字的原因


目的使用自引用结构:

自引用结构 在构建数据结构方面非常有帮助,例如 linked lists ...事实上,变量 next 是在 linked list 中的 nodes 之间建立链接的非常有用的成员 :)

关于c - 在 struct 中使用 struct 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37843509/

相关文章:

c - 当 dlopen one 时,它​​的符号没有被主符号覆盖,为什么?

c++ - 找不到任何 GLU 文档

c++ - struct() 和数组 C++ 的问题

c - 这个声明在 C 中意味着什么

c - 一次调用多个函数的程序不断崩溃。 (C语言编程)

c++ - 如何在不使用数组或任何库函数(任何用于反转的函数)的情况下反转用户输入?

c - Lex(词法分析器)中正则表达式的大问题

c - 对 fork 系统调用行为的一些解释?

c++ - Objective-C 中此数据结构 (C++) 的替代方案

c - 插入/打印时的链表段错误