Union嵌套Struct

1.Union内存开辟空间

typedef union 
{
    long i;
    int k[50];
    char c;
}DATE;

struct date 
{
    int cat;
    DATE cow;
};

struct A 
{
    char c1;
    int data1;
}A;
///////////////////////////////////////////////////////////////////////////////////
printf("%d\n", sizeof(struct date) + sizeof(DATE));
printf("%d\n", sizeof(struct date));
printf("%d\n", sizeof(DATE));
printf("%d\n", sizeof(A));
404
204
200
8

2.Union内存存储

    union data
    {
        struct
        {
            int x, y;
        }s;
        int x, y;
    }d;                                 //1)
    d.x = 1;                            //2)
    d.y = 2;                            //3)
    d.s.x = d.x*d.x;                    //4)
    d.s.y = d.y + d.y;                  //5)
    cout << d.s.x << endl << d.s.y;
    getchar();
}
2.1Union内存存储过程
1)
名称                |值
d                     {s={x=-858993460 y=-858993460}x=-858993460 y=-858993460}

2)
名称                |值
d                     {s={x=1 y=-858993460}x=1 y=1}

3)
名称                |值
d                     {s={x=2 y=-858993460}x=2 y=2}

4)
名称                |值
d                     {s={x=4 y=-858993460}x=4 y=4}

5)
名称                |值
d                      {s={x=4 y=8}x=4 y=4}

你可能感兴趣的:(Union嵌套Struct)