简体   繁体   English

有什么区别:使用关键字 struct 在相同类型的结构内创建结构指针

[英]What's the difference: Creating structure pointer inside a structure of the same type with the keyword struct

Coming from a C++ background here.来自这里的 C++ 背景。 I'm required to write C for my one of my courses, which I've never been exposed to.我必须为我的一门课程编写 C 语言,这是我从未接触过的。 What's' the difference beteween these two declarations?这两个声明之间有什么区别? Why include the struct keyword?为什么要包含 struct 关键字? Are there different meanings?有不同的含义吗? Do they have different meanings in C++ vs C?它们在 C++ 和 C 中有不同的含义吗?

struct A {
    int dataA;
    A* nextA;
};

struct B {
    int dataB;
    struct B* nextB;
};

The comments below say that the first is invalid in C. However, I'm compiling just fine with gcc.下面的评论说第一个在 C 中无效。但是,我用 gcc 编译得很好。

在此处输入图片说明

Edit: I was compiling a .cpp file with gcc.编辑:我正在用 gcc 编译一个 .cpp 文件。 I'm such a noob lol.我是个菜鸟,哈哈。

Solution: The the second is required for C where the first is only valid in C++.解决方案: C 需要第二个,其中第一个仅在 C++ 中有效。

struct B {
    int dataB;
    B* nextB;
};

is the same as是相同的

struct B {
    int dataB;
    struct B* nextB;
};

in C++ but not in C. When using C, you'll have to use the second form.在 C++ 中而不是在 C 中。使用 C 时,您必须使用第二种形式。 The first form is not legal C.第一种形式不合法 C.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM