我已经浏览了这篇非常好的文章
https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule
但是我不了解使用 union 的别名,我对别名的了解是 with char/ unsigned char
,根据这篇文章,这是一个编译器异常。所以,我过去是如何做混叠的char/ unsigned char
,
// lets say I have struct blah , having int member non_sense, cmmn_sense, intelligence...
struct blah *foo, *foo2;
unsigned char buffer [512];
foo = (struct blah *) (buffer+0);
//now accessing members
foo->non_sense = -1;
foo->non_snese = 0;
foo->intelligence =1;
// now we can use another struct after that ..
foo2 = (struct blah*) (buffer + sizeof(struct blah));
如果我在某些地方错了,请纠正我:)
问:那么我怎样才能对 union 做同样的事情呢?
注意我没有遇到多少联合,所以我也不知道它的正确用途。
我也搜索过别名enum
,但不太了解,
问:如果可能的话,我们将如何使用枚举?
对不起我的英语,不是那么好,请公开建议纠正我在问题中的误解或术语以纠正......