在函数调用中struct Node*和Node*有什么区别?

the*_*owd 2 c++ struct scope declaration

传递给function1和function2的区别是什么?

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

void function1(struct Node *start)
{
    // ...
}

void function2(Node *start)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

Bat*_*eba 5

除了可能作为前瞻性声明(在你的情况下是一个没有实际意义的点)之外,没有任何区别.您struct Node*在函数参数列表中的使用是对C语言中的需求的致敬.

您不需要在C++中这样做,因为它有一种不同的方式将其命名空间组织到C语言.

有关该构造的更正式的解释,请参阅http://en.cppreference.com/w/cpp/language/elaborated_type_specifier


Sto*_*ica 5

实质上的区别在于,当先前未在函数声明/定义点声明类型时会发生什么.

只要function1不尝试访问对象的任何成员,就不需要存在类型定义或声明.它具有将声明struct Node引入封闭范围的效果.

function2确实需要它(或在某个地方存在前向声明),无论它对象的作用如何.它没有隐式引入类类型.

  • @Quentin记得,无论你的日子多么糟糕,那里有人必须将Microsoft Edge移植到Android. (7认同)
  • @Quentin这不是真的错.没有任何'Node`声明,`function1`有效,`function2`没有.您正在考虑问题中不存在的`struct Node;`或`typedef struct Node Node;`. (4认同)