我想知道,有什么区别:
struct Node
{
int data;
Node *next;
};
Run Code Online (Sandbox Code Playgroud)
和
struct Node
{
int data;
struct Node *next;
};
Run Code Online (Sandbox Code Playgroud)
为什么我们struct在第二个例子中需要关键字?
另外,有什么区别
void Foo(Node* head)
{
Node* cur = head;
//....
}
Run Code Online (Sandbox Code Playgroud)
和
void Foo(struct Node* head)
{
struct Node* cur = head;
//....
}
Run Code Online (Sandbox Code Playgroud)
只有包含的声明struct在 C 中有效。在 C++ 中没有区别。
但是,您可以typedef使用structC 语言,因此您不必每次都编写它。
typedef struct Node
{
int data;
struct Node *next; // we have not finished the typedef yet
} SNode;
SNode* cur = head; // OK to refer the typedef here
Run Code Online (Sandbox Code Playgroud)
为了兼容性,此语法在 C++ 中也有效。