【发布时间】:2014-07-24 15:45:26
【问题描述】:
我不确定这是否与编译相关,但我使用的编译器是 IAR 7.10.3。
我有一个如下结构:
struct A {
struct {
uint8_t x:1;
uint8_t y:2;
uint8_t z:5;
} b;
};
并像这样初始化它:
struct A a = {
.b = 0xFF,
};
现在当我查看内存中的结构时,只有 x 位将设置为 '1',其余的将为零。
这是它应该按照 C 标准的行为方式吗?
【问题讨论】:
-
这类似于this最近提出的问题。
-
@this 他正在使用
IAR,位域类型可以是实现定义的类型。 -
@this c99 说
A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type....注意实现定义的类型。 -
@NiklasNorin 我引用的第 17 段涵盖了这种情况:
causes the following initializer to begin initialization of the subobject described by the designator. Initialization then continues forward in order, beginning with the next subobject after that described by the designator. -
顺便说一句,您的内部结构不是 anonymous 结构。 c11 将匿名结构定义为未命名且未标记的内部结构。
标签: c