函数参数中的struct关键字有什么区别?

时间:2015-07-01 08:38:10

标签: c++ c struct

我想知道,有什么区别:

struct Node
{
  int data;
  Node *next;
};

struct Node
{
  int data;
  struct Node *next;
};

为什么我们在第二个例子中需要struct个关键字?

另外,

之间有什么区别
void Foo(Node* head) 
{
    Node* cur = head;
    //....
}

void Foo(struct Node* head) 
{
    struct Node* cur = head;
    //....
}

2 个答案:

答案 0 :(得分:4)

只有包含struct的声明在C中有效.C ++没有区别。

但是,您可以在{C}中typedef struct,因此您不必每次都写它。

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

此语法在C ++中也是有效的兼容性。

答案 1 :(得分:0)

Struct节点是我们创建的新用户定义数据类型。与类不同,使用结构的新数据类型是" struct strct_name" ,即;你需要在struct_name前面的关键字struct。 对于类,您不需要在新数据类型名称前面添加关键字。 例如;

class abc
{
  abc *next;
};

当你宣布变量时

abc x;

而不是struct

abc x;

如果是结构。 也可以通过声明了解

struct node * next;

我们正在尝试创建一个指向" strcut node"类型的变量的指针,在这种情况下称为自引用指针,因为它指向父结构。