【发布时间】:2016-06-19 04:15:10
【问题描述】:
我还是 C 的新手。我知道您可以将已经声明的 struct 用作新的数据类型,例如 int、double 等。但是,我遇到了这样写的 struct :
struct AdjListNode
{
int dest;
int weight;
struct AdjListNode* next;
};
在这个struct中,“next”指针的数据类型是struct AdjListNode*。 struct 与已经声明的 AdjListNode* 有什么关系?谢谢!
【问题讨论】:
-
你到底在问什么?
-
它是一个指向这个结构的实例的指针,也可以是self。在这个特定的例子中,它看起来像一个链表,所以 next 将指向链中的下一个结构......
-
AdjListNode不是数据类型。这是一个结构标签。struct AdjListNode是实际的数据类型。struct AdjListNode *next;将next声明为指向struct AdjListNode的指针。 -
就您的代码而言,
struct AdjListNode是引用该结构的唯一有效方式。只有在 C++ 中,您才能将结构简单地称为AdjListNode。在plain-old-C 中,您需要一个typedef,然后才能引用没有struct关键字的结构。