在 c 中访问联合成员 [英] access union member in c
本文介绍了在 c 中访问联合成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个关于c语言union的问题
I have a question about union in c language
例如:
typedef struct {
int a;
float c;
}Type1;
typedef struct {
int b;
char d;
}Type2;
union Select {
Type1 type1;
Type2 type2;
};
void main() {
Select* select;
//can we access type1 first and then access type2 immediately? like this way:
select->type1.a;
select->type2.b;
//访问type1后,立即访问type2,能得到type2的值b吗?//我对第一篇文章做了一点修改,因为开头毫无意义.
//after access type1, and then access type2 immediately, can we get the value b of type2? //I modify the first post a little bit, because it is meanless at the beginning.
}
推荐答案
是的,没错.在您的示例中(忽略未初始化的指针)type1.a
和 type2.b
的值对于任何给定的 Select
.
Yes, that is correct. In your example (ignoring the uninitialised pointer) the value of type1.a
and type2.b
will always be the same for any given instance of Select
.
这篇关于在 c 中访问联合成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文