【发布时间】:2019-12-16 08:38:18
【问题描述】:
假设我有一个结构,ivec2:
typedef struct ivec2 {
int x, y;
} ivec2;
我想知道我是否可以建立类似于以下的联合:
union rectangle {
ivec2 size; // 8 bytes; members: int x, y;
int width, height; // 4 + 4 bytes
};
其中width 对应于size.x,height 对应于size.y。
我已经看到可以这样做:
union rectangle {
ivec2 size; // 8 bytes
int arr[2]; // 4 + 4 bytes
};
但是我可以使用单独的成员吗?
这张图片显示了我的意思:
【问题讨论】:
-
不,您不能对工会的个人成员这样做,因为工会的每个个人成员都与其他所有成员共享空间。
标签: c data-structures struct unions