我不明白这个嵌套结构 [英] I don't understand this nested struct

查看:28
本文介绍了我不明白这个嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结构是:

struct 
{    
    int x;
    struct 
    {
        int y, z;
    } nested;
}
i={.nested.y = 5, 6, .x = 1, 2};

问题:结果是 i.nested.y = 2;i.nested.z = 6;

Question: The result is i.nested.y = 2; i.nested.z = 6;

我无法理解或理解这一点,内部也是一个结构体但联合体,所以有人可以帮助我吗?

I cannot get or understand this, the inner one is also a struct but union, so could anyone help me?

推荐答案

好吧,首先让我说这是一种很糟糕的初始化结构的方式,如果这是一个家庭作业问题 - 这也不是一个很好的问题.
然而,考虑到手头的问题,我认为初始化过程中发生的事情是这样的:首先,y 被初始化为 5,作为结构中的下一个成员的 z 被初始化为 6.然后,x 被初始化为 1结构中的下一个成员,在本例中为 y,被初始化为 2(覆盖 5).

Ok, first of all let me say that this is a terrible way to initialize a struct, and if it's a homework question - it's not a very good one either.
However, given the problem at hand, what I believe is happening in the initialization is this: first, y is initialized to 5, and z, being the next member in the struct, is initialized to 6. Then, x is initialized to 1 and the next member in the struct, in this case y, is initialized to 2 (which overwrites the 5).

还有一点需要注意,嵌套结构不是联合,而是结构——首先,因为这是代码所说的,而且因为如果它是联合——这将是不可能的这种情况下 y 和 z 具有不同的值,因为它们都将保存在内存中的同一位置.

Something important to note as well, is that the nested struct isn't a union, but a struct - first of all, because that's what the code says, but also because if it were a union - it would be impossible in this case for y and z to have different values, since both would be kept in the same location in the memory.

这篇关于我不明白这个嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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