指向结构的指针的语法

Syntax for pointers to a structure

当声明一个指向结构的指针时,以下两个代码片段都可以正确编译:

A)

struct Foo
{
int data;
Foo* temp;   // line in question
}

B)

struct Foo
{
int data;
struct Foo* temp;  //line in question
}

在struct指针的声明中重复“struct”这个词有什么意义(如(B))?与不这样做相比(如(A))有什么区别吗?

感谢您的考虑。

在C中,struct关键字必须使用来声明结构变量,但可选 在 C++ 中。

例如,请考虑以下示例:

struct Foo
{
    int data;
    Foo* temp; // Error in C, struct must be there. Works in C++
};
int main()
{
    Foo a;  // Error in C, struct must be there. Works in C++
    return 0;
}

示例 2

struct Foo
{
    int data;
    struct Foo* temp;   // Works in both C and C++
};
int main()
{
    struct Foo a; // Works in both C and C++
    return 0;
}

在上面的例子中,temp 是一个 指向 non-const Foo.

的数据成员