我可以使用联合来表达结构和多个打包成员吗?
Can I use a union to express a struct and multiple packed members?
假设我有一个结构,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
};
但是我可以和不同的成员一起做吗?
这张图片显示了我的意思:
您要做的是在联合中嵌套一个匿名结构。
而不是:
union rectangle {
ivec2 size;
int width, height;
};
做:
union rectangle {
ivec2 size;
struct {
int width;
int height;
};
};
假设我有一个结构,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
};
但是我可以和不同的成员一起做吗?
这张图片显示了我的意思:
您要做的是在联合中嵌套一个匿名结构。
而不是:
union rectangle {
ivec2 size;
int width, height;
};
做:
union rectangle {
ivec2 size;
struct {
int width;
int height;
};
};