简体   繁体   English

C:如何访问不同类型的匿名或未命名的嵌套结构

[英]C: How to access different types of anonymous or unnamed nested structs

I noticed that there are a few ways to define structs inside other structs in C: 我注意到有几种方法可以在C中的其他结构中定义结构:

struct s {
    int abc;

    struct {
        int a;
    };

    struct {
        int b;
    } intern;

    struct i {
        int c;
    };

    struct i2 {
        int d;
    } intern2;

    struct i3 {
        int e;
    };
    struct i3 intern3;
};

This struct compiles fine using gcc or g++, so I assume all parameters are accessible in some way. 这个结构使用gcc或g ++编译很好,所以我假设所有参数都可以通过某种方式访问​​。 I tried it like this: 我试过这样的:

int main(int argc, char const *argv[])
{
    struct s mystruct;

    mystruct.abc = 0;
    mystruct.a = 1;
    mystruct.intern.b = 2;
    mystruct.c = 3; // <-- does not compile
    mystruct.intern2.d = 4;
    mystruct.intern3.e = 5;

    return 0;
}

Apart from the access mystruct.c , everything compiles (compile error message is 'struct s' has no member named 'c' ). 除了访问mystruct.c ,所有内容都编译(编译错误消息'struct s' has no member named 'c' )。 Am I accessing the struct parameters in the correct way? 我是否以正确的方式访问struct参数? Are there alternatives? 还有替代品吗? How do I access the c parameter? 我如何访问c参数?

In your code, 在你的代码中,

struct i {
        int c;
    };

there is no member variable of type struct i neither it qualifies for anonymous structure Note . 没有类型为struct i成员变量,它既不符合匿名结构 注释 If you create a variable of that type, you'll be able to access the member variable c , similar to what you've done for struct i3 with intern3 variable. 如果您创建该类型的变量,您将能够访问成员变量c ,类似于您使用intern3变量对struct i3所做的操作。

Adding a bit regarding the error message you're seeing, 添加一点关于您看到的错误消息,

struct s has no member named c struct s没有名为c成员

because, in case of an anonymous structure, the members are considered a direct member of the containing structure. 因为,在匿名结构的情况下,成员被视为包含结构的直接成员。 In case of a struct definition with a tag , that structure is not an anonymous structure and the member of the structure needs a structure variable of that type to be accessed. 在带有标记struct定义的情况下,该结构不是匿名结构,并且结构的成员需要要访问的该类型的结构变量。


NOTE: 注意:

Regarding anonymous structure , quoting C11 , chapter §6.7.2.1, ( emphasis mine ) 关于匿名结构 ,引用C11 ,章节§6.7.2.1,( 强调我的

An unnamed member whose type specifier is a structure specifier with no tag is called an anonymous structure ; 类型说明符是没有标记结构说明符的未命名成员称为匿名结构 ; an unnamed member whose type specifier is a union specifier with no tag is called an anonymous union. 一个未命名的成员,其类型说明符是一个没有标记的联合说明符,称为匿名联合。 The members of an anonymous structure or union are considered to be members of the containing structure or union. 匿名结构或联合的成员被视为包含结构或联合的成员。 This applies recursively if the containing structure or union is also anonymous. 如果包含的结构或联合也是匿名的,则递归应用。

From the gcc docs : 来自gcc文档

As permitted by ISO C11 and for compatibility with other compilers, GCC allows you to define a structure or union that contains, as fields, structures and unions without names. 在ISO C11允许的情况下,为了与其他编译器兼容,GCC允许您定义一个结构或联合,其中包含没有名称的字段,结构和联合。 For example: 例如:

 struct {
   int a;
   union {
     int b;
     float c;
   };
   int d;
 } foo;

In this example, you are able to access members of the unnamed union with code like 'foo.b'. 在此示例中,您可以使用“foo.b”之类的代码访问未命名联合的成员。

You were able to get at mystruct.a because the structure had no tag. 你能够得到mystruct.a因为结构没有标签。 You can't get at your mystruct.c because the containing struct for c has the tag i . 你不能得到你的mystruct.c因为c的包含结构有标签i

Change: 更改:

 struct i {
        int c;
    };

to

struct {
    int c;
}

and you should be able to get at mystruct.c . 你应该能够得到mystruct.c

Member c is declared inside inside strut i . 成员cstrut i内部声明。 And you are not creating any variable for struct i . 而且你没有为struct i创建任何变量。

So, you can't access member c . 因此,您无法访问成员c

When you mention mystruct.c , it expects c to be a member of structure s , which is not. 当你提到mystruct.c ,它希望c是结构s的成员,而不是。

If you don't name your dog, you can't call it 如果您没有为您的狗命名,则无法拨打电话

That is exactly what unnamed says - it has no name, so you cannot access it by name. 这正是未命名的内容 - 它没有名称,因此您无法通过名称访问它。

The only reason to have unnamed members in a structure is if you are mirroring a structure given externally, and you don't care about specific pieces of it, so you name only the pieces you want to access. 在结构中包含未命名成员的唯一原因是,如果您正在镜像外部给定的结构,并且您不关心它的特定部分,那么您只要命名要访问的部分。

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

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