sohappyoh 发表于 2014-6-10 08:56:51

如何将联合体成员定位!

如何将联合体成员定位!

struct mis_t{    unsigned char a;    unsigned int b;    unsigned long c;}; union{    struct mis_t misu;!    unsigned int e;}misu;


如上:如何将联合体 “misu” 的成员“unsigned int e” 定位到结构体的 “struct mis_t misu” 的 “unsigned int b” 成员。不要用摆放位置的方法定位可以吗?

albert_w 发表于 2014-6-10 09:22:48

没跟上这个场景, 既然这样,那何不直接操作结构体内的unsigned int b呢, 联合都不需要了, 标记下结构体类型即可。

lixuyongzd 发表于 2014-6-10 10:06:07

struct mis_t
{
    unsigned int b;
    unsigned char a;
    unsigned long c;
};
union
{
    struct mis_t misu;
    unsigned int e;
}misu;
试试吧

qlb1234 发表于 2014-6-10 10:36:45

Negative.
The members of a union is actually the same person. They're all pointing to a same address. The only difference is they have different types.

sohappyoh 发表于 2014-6-10 10:40:18

lixuyongzd 发表于 2014-6-10 10:06
struct mis_t
{
    unsigned int b;


不是说了吗,不要用位置的方法啊,因为我存储的是EEProm,这样的话要改很多地址的

millwood0 发表于 2014-6-10 23:40:59

不要用摆放位置的方法定位可以吗?

That happens to be the simplest and best approach.

Otherwise,

1) use another struct;
2) padding.
页: [1]
查看完整版本: 如何将联合体成员定位!