如何在C中返回匿名结构? [英] How to return an anonymous struct in C?

查看:64
本文介绍了如何在C中返回匿名结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试一些代码后,我意识到可以编译以下代码:

Trying some code I realized that the following code compiles:

struct { int x, y; } foo(void) {
}

似乎我们在定义一个名为 foo ,它返回匿名的 struct

It seems as if we are defining a function named foo which returns an anonymous struct.

现在,我的问题是:它是否只在使用我的编译器进行编译时使用?或者这是合法的C(99)?如果是这样,则return语句的正确语法是什么,如何正确地将返回值分配给变量?

Now, my question is: Does it only happen to compile with my compiler or is this legal C(99)? If so, what is the correct syntax for a return statement and how can I correctly assign the returned value to a variable?

推荐答案

您要返回的结构不是匿名结构。 C标准将匿名结构定义为不使用标记的另一个结构的成员。您返回的是没有标签的结构,但是由于它不是成员,因此不是匿名的。 Gcc使用名称​​<匿名> 表示没有标签的结构。

The struct you're returning is not an anonymous struct. C standard defines an anonymous struct as a member of another struct that doesn't use a tag. What you're returning is a struct without a tag, but since it isn't a member, it is not anonymous. Gcc uses the name < anonymous > to indicate a struct without a tag.

假设您尝试在函数中声明相同的结构。

Let's say you try to declare an identical struct in the function.

struct { int x, y; } foo( void )  
{
    return ( struct { int x, y; } ){ 0 } ;
}

gcc抱怨:返回类型'struct<时不兼容的类型。匿名>但结构<

显然类型不兼容。在标准中,我们看到:

Apparently the types are not compatible. Looking in the Standard we see that:


6.2.7兼容类型和复合类型

6.2.7 Compatible type and composite type

1:两种类型具有兼容类型(如果它们的类型相同)。用于确定两种类型是否兼容的其他规则,在6.7.2中针对类型说明符进行了描述,在6.7.3中针对类型限定符进行了说明,在6.7.6中针对声明符进行了描述。 此外,在单独的转换中声明的两个结构,联合或枚举类型如果它们的标记和成员满足以下要求,则它们是兼容的:如果一个标记为,则另一个标记必须相同。标签。 如果两者都在各自翻译单位内的任何位置完成,那么以下附加要求适用:它们的成员之间应存在一对一的对应关系,以便每对对应的成员都声明为兼容类型;如果该对中的一个成员是使用对齐方式说明符声明的,则另一个使用等效的对齐方式说明符声明;如果该对中的一个成员用名称声明,则另一个用相同名称声明。对于两个结构,相应的成员应以相同的顺序声明。对于两个结构或联合,相应的位域应具有相同的宽度。对于两个枚举,对应的成员应具有相同的值。

1: Two types have compatible type if their types are the same. Additional rules for determining whether two types are compatible are described in 6.7.2 for type specifiers, in 6.7.3 for type qualifiers, and in 6.7.6 for declarators. Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if their tags and members satisfy the following requirements: If one is declared with a tag, the other shall be declared with the same tag. If both are completed anywhere within their respective translation units, then the following additional requirements apply: there shall be a one-to-one correspondence between their members such that each pair of corresponding members are declared with compatible types; if one member of the pair is declared with an alignment specifier, the other is declared with an equivalent alignment specifier; and if one member of the pair is declared with a name, the other is declared with the same name. For two structures, corresponding members shall be declared in the same order. For two structures or unions, corresponding bit-fields shall have the same widths. For two enumerations, corresponding members shall have the same values.

第二个粗体部分说明如果两个struct都没有标记,例如例如,他们必须遵循该部分之后列出的其他要求。但是,如果您注意到第一个粗体部分必须位于单独的翻译单元中,那么示例中的结构就不必了。因此,它们是不兼容的,并且代码无效。

The second bold part, explains that if both struct are without the tag, such as in this example, they have to follow additional requirements listed following that part, which they do. But if you notice the first bold part they have to be in separate translation units, structs in the example aren't. So they are not compatible and the code is not valid.

不可能正确编写代码,因为如果声明一个struct并在此函数中使用它,则必须使用标记,这违反了两个结构都必须具有相同标记的规则:

It is impossible to make the code correct since if you declare a struct and use it in this function, you have to use a tag, which violates the rule that both have structs have to have the same tag:

struct t { int x, y; } ;

struct { int x, y; } foo( void )   
{
    struct t var = { 0 } ;
    
return var ;
}

再次gcc抱怨:返回类型'struct t'但返回'struct< ;

Again gcc complains: incompatible types when returning type 'struct t' but 'struct < anonymous >' was expected

这篇关于如何在C中返回匿名结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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