C++ 结构名称中的指针

Pointer in c++ struct name

本文关键字:指针 结构 C++      更新时间:2023-10-16

我遇到了这段用于 c++ 链表实现的代码。

struct node
{
    int info;
    struct node *next;
}*start;

拥有*start而不仅仅是start意味着什么?

以后这样使用会怎样?s是什么意思,它没有在函数中的其他任何地方引用?

struct node *temp, *s;
temp = new(struct node); 

片段

struct node
{
    int info;
    struct node *next;
}*start;

相当于

struct node
{
    int info;
    struct node *next;
};
struct node *start;

因此,在第一个片段中,您可以在一个语句中定义一个名为 node 的结构和一个名为 startstruct node * 类型的变量。就这样。

请注意,在C++中(与 C 不同(,您也可以编写

struct node
{
    int info;
    node *next;
};
node *start;

即,在定义类型为 struct node 的变量时,您可以省略 struct -关键字。