在结构中引用未定义的类型如何合法? [英] How is it legal to reference an undefined type inside a structure?

查看:18
本文介绍了在结构中引用未定义的类型如何合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为回答另一个问题的一部分,我遇到了一段这样的代码,gcc 编译时毫无怨言.

As part of answering another question, I came across a piece of code like this, which gcc compiles without complaint.

typedef struct {
    struct xyz *z;
} xyz;
int main (void) {
    return 0;
}

这是我一直用来构造指向自身的类型(例如,链表)的方法,但我一直认为您必须命名结构,以便您可以使用 self-参考.换句话说,您不能在结构中使用 xyz *z,因为此时 typedef 尚未完成.

This is the means I've always used to construct types that point to themselves (e.g., linked lists) but I've always thought you had to name the struct so you could use self-reference. In other words, you couldn't use xyz *z within the structure because the typedef is not yet complete at that point.

但是这个特定的示例没有命名结构并且它仍然可以编译.我原以为编译器会自动翻译上述代码,因为结构和 typedef 名称相同.

But this particular sample does not name the structure and it still compiles. I thought originally there was some black magic going on in the compiler that automatically translated the above code because the structure and typedef names were the same.

但这个小美人也能用:

typedef struct {
    struct NOTHING_LIKE_xyz *z;
} xyz;

我在这里错过了什么?这似乎是一个明显的违规行为,因为在任何地方都没有定义 struct NOTHING_LIKE_xyz 类型.

What am I missing here? This seems a clear violation since there is no struct NOTHING_LIKE_xyz type defined anywhere.

当我将其从指针更改为实际类型时,出现预期错误:

When I change it from a pointer to an actual type, I get the expected error:

typedef struct {
    struct NOTHING_LIKE_xyz z;
} xyz;

qqq.c:2: error: field `z' has incomplete type

此外,当我删除 struct 时,我收到一个错误(parse error before "NOTHING ...).

Also, when I remove the struct, I get an error (parse error before "NOTHING ...).

这在 ISO C 中是否允许?

Is this allowed in ISO C?

更新:struct NOSUCHTYPE *variable; 也可以编译,因此它不仅在 inside 结构中似乎有效.我在 c99 标准中找不到任何允许对结构指针进行这种宽大处理的内容.

Update: A struct NOSUCHTYPE *variable; also compiles so it's not just inside structures where it seems to be valid. I can't find anything in the c99 standard that allows this leniency for structure pointers.

推荐答案

你所追求的 C99 标准的部分是 6.7.2.3,第 7 段:

The parts of the C99 standard you are after are 6.7.2.3, paragraph 7:

如果表单的类型说明符struct-or-union identifier 出现除了作为上述之一的一部分表格,并且没有其他声明作为标签的标识符是可见的,然后它声明了一个不完整的结构或联合类型,并声明标识符作为该类型的标记.

If a type specifier of the form struct-or-union identifier occurs other than as part of one of the above forms, and no other declaration of the identifier as a tag is visible, then it declares an incomplete structure or union type, and declares the identifier as the tag of that type.

...和 ​​6.2.5 第 22 段:

...and 6.2.5 paragraph 22:

未知的结构或联合类型内容(如 6.7.2.3 中所述)是不完整的类型.完成了,对于该类型的所有声明,通过声明相同的结构或联合带有定义内容的标签相同的范围.

A structure or union type of unknown content (as described in 6.7.2.3) is an incomplete type. It is completed, for all declarations of that type, by declaring the same structure or union tag with its defining content later in the same scope.

这篇关于在结构中引用未定义的类型如何合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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