C语言 访问结构内联合中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3271248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:55:15  来源:igfitidea点击:

Accessing variables in a union inside a struct

cstructunions

提问by Nitzs

Can anyone please explain why the 1st method of accessing a nested struct element inside an union in a struct works and the 2nd does not?

任何人都可以解释为什么访问结构中联合内部嵌套结构元素的第一种方法有效而第二种方法无效?

typedef struct element Node;
struct element
{
    int type;
    union
    {
        int value;
        Node *child[2];
    } u;
};
int main()
{
    Node n;
    Node *p;    
    n.type = 0;
    p = n.u.child[0];
    p->type = 10;  // 1st method
    (n.u.child[1])->type = 24;   // 2nd method
    return 0;
}

回答by Amardeep AC9MF

Try the following:

请尝试以下操作:

int main()
{
    Node n;
    Node *p;    
    n.type = 0;

    // allocate memory for child nodes
    n.u.child[0] = (Node *)malloc(sizeof(Node));

    if (n.u.child[0] == NULL)
    {
        return 1;
    }

    n.u.child[1] = (Node *)malloc(sizeof(Node));

    if (n.u.child[1] == NULL)
    {
        free(n.u.child[0]);
        return 1;
    }

    p = n.u.child[0];
    p->type = 10;  // 1st method
    (n.u.child[1])->type = 24;   // 2nd method

    // release dynamically allocated memory
    free(n.u.child[0]);
    free(n.u.child[1]);

    return 0;
}

NOTE:Don't modify n.u.valueof a Node if you've already assigned its child[] pointers. You will overwrite one of the pointers and leak that memory as well as crash if you try to access the child[] array after that. Unions are tricky -- best to avoid this sort of arrangement.

注意:n.u.value如果您已经分配了它的 child[] 指针,请 不要修改Node。如果您在此之后尝试访问 child[] 数组,您将覆盖其中一个指针并泄漏该内存以及崩溃。工会很棘手——最好避免这种安排。

回答by Jens Gustedt

Your problem has not much to do with the fact that there are unions involved.

您的问题与union涉及 s的事实没有太大关系。

Accessing uninitialized pointers just gives you random behavior. Sometimes it does work sometimes not. For your first access probably something just luckily happened to be in the place that you access.

访问未初始化的指针只会给你随机行为。有时它确实有效有时无效。对于您的第一次访问,可能只是幸运地发生在您访问的地方。

Just initialize, à la C99:

只需初始化,如 C99:

Node n = { .type = 0 };

or

或者

Node n = { 0 };

à la C89, instead of your assignment statement. This has the advantage to initialize all components that are not mentioned to 0, thus your pointers. Then your test code should segfault happily ever after.

à la C89,而不是你的赋值语句。这具有将所有未提及的组件初始化为 0 的优点,因此您的指针。那么你的测试代码应该从此愉快地出现段错误。

回答by Mark Elliot

Either of those methods shouldbe fine for accessing a nested struct element inside a union, the issue here is that you haven't allocated memory for the nodes referred to by child[0] or child[1]. (I'm surprised your "1st method" doesn't fail, too.)

这两种方法中的任何一种都应该适合访问联合内部的嵌套结构元素,这里的问题是您没有为 child[0] 或 child[1] 引用的节点分配内存。(我很惊讶你的“第一种方法”也没有失败。)