结构体并以两种不同的方式引用它们
我正在阅读某人的代码,我不明白为什么他在下面的每个联合中引用的内容不同。
-在第一个上,他可以通过输入pwr_cmd.EN1
来引用内部 -在第二个中,他可以通过输入 errors.Bits.ERR1 来引用内部 -如果我从第二个中删除Bits
,我是否能够以与第一个相同的方式引用内部:errors.ERR1
? - 两者之间有什么区别?为什么你会选择其中一种而不是另一种?谢谢。
typedef union {
byte Byte;
union {
struct {
byte EN1 :1;
byte EN2 :1;
byte EN3 :1;
byte EN4 :1;
byte :4;
};
struct {
byte :2;
byte EN34 :2;
byte :4;
};
struct {
byte :2;
byte EN3r:1;
byte EN4r:1;
byte :4;
};
};
} pwr_cmd;
typedef union {
word Word;
union {
struct {
byte ERR1 :1;
byte ERR2 :1;
byte ERR3 :1;
byte ERR4 :1;
byte ERR5 :1;
byte ERR6 :1;
byte ERR7 :1;
byte ERR8 :1;
byte ERR9 :1;
byte DET1 :1;
byte DET2 :1;
byte FAIL1 :1;
byte PWR1 :1;
byte PWR2 :1;
byte PWR3 :1;
byte PWR4 :1;
} Bits;
nibble4 Nibble4;
};
} errors;
I am reading someone's code I do not understand why he is referencing inside each union below differently.
-On the first one, he is able to reference inside by typing pwr_cmd.EN1
-On the second one, he is able to reference inside by typing errors.Bits.ERR1
-If I remove the Bits
from the second one, will I be able to reference inside the the same way as the first one: errors.ERR1
? -What is the difference between the two and why would you do one over the other? Thank you.
typedef union {
byte Byte;
union {
struct {
byte EN1 :1;
byte EN2 :1;
byte EN3 :1;
byte EN4 :1;
byte :4;
};
struct {
byte :2;
byte EN34 :2;
byte :4;
};
struct {
byte :2;
byte EN3r:1;
byte EN4r:1;
byte :4;
};
};
} pwr_cmd;
typedef union {
word Word;
union {
struct {
byte ERR1 :1;
byte ERR2 :1;
byte ERR3 :1;
byte ERR4 :1;
byte ERR5 :1;
byte ERR6 :1;
byte ERR7 :1;
byte ERR8 :1;
byte ERR9 :1;
byte DET1 :1;
byte DET2 :1;
byte FAIL1 :1;
byte PWR1 :1;
byte PWR2 :1;
byte PWR3 :1;
byte PWR4 :1;
} Bits;
nibble4 Nibble4;
};
} errors;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是位域的示例 - 每个带有“:”的变量表示整数的一定数量的位。该位字段被联合化,以允许根据上下文对相同的位位置进行不同的命名。
为了清楚起见并表示代码的意图,您可以访问一个联合成员而不是另一个成员 - 这是自我记录代码的好方法,通过创建有意义的名称而不是像“bit 4”这样的无意义的名称。
重新阅读您的问题 - 两者之间的区别在于,一个使用命名结构,另一个不使用命名结构。在第一种情况下,无需执行结构成员引用(“.”)即可访问结构成员,因为它们没有名称。在第二个中,他声明了一个带有名称的结构,因此使用了结构成员引用。我认为第二个更清晰,但第一个更简洁。这是风格和偏好的问题。
This is an example of a bitfield - each variable with a ":" represents a certain number of bits of an integer. This bitfield is unionized to allow the same bit positions to be named differently depending on context.
You would access one union member over another for clarity and to denote the intention of your code - this is a good way to self-document code, by creating meaningful names instead of something meaningless like "bit 4".
Re-reading your question - the difference between the two is that one uses named structs and the other doesn't. In the first, the members of the structs can be accessed without doing a struct member reference ("."), because they have no name. In the second, he declares a struct with a name, so the struct member reference is used. In my opinion the second is more clear, but the first is more concise. It's a matter of style and preference.