为什么 "typdef struct { struct S *s;} S;"包含指向相同类型编译的指针? [英] Why does "typdef struct { struct S *s; } S;" containing a pointer to same type compile?

查看:20
本文介绍了为什么 "typdef struct { struct S *s;} S;"包含指向相同类型编译的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 typedef 一个 struct,它包含一个指向另一个相同类型的指针.

I'm trying to typedef a struct which contains a pointer to another of the same type.

这就是我认为最好的版本:

typedef struct Element
{
    char value;
    struct Element *next;
} Element;

为什么该变体也在编译 + 执行?:

typedef struct
{
    char value;
    struct Element *next;
} Element;

为了描述第一个,我会说:Name struct Element Element now" 第二个描述为:Take这个匿名的 struct 并称之为 Element"

To describe the first I'd say: "Name struct Element Element now" and the second as: "Take this anonymous struct and call it Element"

但是为什么我仍然可以在第二种情况下声明一个 struct Element (在结构体内部)?

But why can I still declare a struct Element (inside the struct) in the second case?

(在 GCCMSVC 中工作)

(Working in GCC and MSVC)

推荐答案

在第一种情况下,您的结构体有两个等效名称:struct Element(其中 Element 是一个struct 标记)和 Element(其中 Element 是 typedef,现有类型的别名).

In the first case, your struct has two equivalent names: struct Element (where Element is a struct tag) and Element (where Element is a typedef, an alias for an existing type).

在第二种情况下,您只是没有为结构定义标签.通常这将是完全有效的,但在这里您指的是 next 成员声明中不存在的类型 struct Element.

In the second case, you just didn't define a tag for the struct. Normally that would be perfectly valid, but here you're referring to the nonexistent type struct Element in the declaration of the next member.

在这种情况下,struct Element 是一种不完整类型.您不能声明不完整类型的对象,但可以声明指向它们的指针.

In that context, struct Element is an incomplete type. You can't declare objects of incomplete types, but you can declare pointers to them.

声明

typedef struct
{
    char value;
    struct Element *next;
} Element;

是合法的,但它不会使 next 成为指向封闭类型的指针.它使它成为指向某个不完整类型的指针,除非声明完整类型,否则您将无法引用它.

is legal, but it doesn't make next a pointer to the enclosing type. It makes it a pointer to some incomplete type, and you won't be able to refer to it until and unless you declare the full type.

您的第二个声明是许多没有意义但仍然合法的 C 声明之一.

Your second declaration is one of the plethora of things that don't make sense, but are still legal C.

可能考虑只省略 typedef 并始终将类型称为 struct Element.由于很多人喜欢为结构类型使用一个单词名称的便利性,但我个人认为这样做并没有太大好处(除非该类型确实是不透明的,即该类型的用户甚至不知道它是一个结构).这是风格问题.

You might consider just omitting the typedef and consistently referring to the type as struct Element. As lot of people like the convenience of having a one-word name for a structure type, but my own personal opinion is that there's not much benefit to that (unless the type is truly opaque, i.e., users of the type don't even know it's a struct). It's a matter of style.

请注意,在定义本身中,您需要将类型称为 struct Element,而不是 Element,因为 typedef 名称为 Element尚不可见.

Note that you need to refer to the type as struct Element, not Element, within the definition itself, since the typedef name Element isn't visible yet.

struct 标记和 typedef 具有相同名称的事实可能看起来令人困惑,但它完全合法.Struct 标签和 typedef 位于不同的命名空间中(在 C 意义上,而不是 C++ 意义上);struct 标签只能出现在 struct 关键字之后.

The fact that the struct tag and the typedef have the same name may seem confusing, but it's perfectly legititimate. Struct tags and typedefs are in separate namespaces (in the C sense, not the C++ sense); a struct tag can only appear immediately after the struct keyword.

另一种选择是将 typedef 与结构定义分开:

Another alternative is to separate the typedef from the struct definition:

typedef struct Element Element;

struct Element {
    char value;
    Element *next;
};

(您可以在 typedef 中使用不完整的类型名称.)

(You can use an incomplete type name in a typedef.)

这篇关于为什么 "typdef struct { struct S *s;} S;"包含指向相同类型编译的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆