小编use*_*904的帖子

C中的Bitfields和Unions给出了问题

我正在实施无线电标准,并在结构和内存大小方面遇到了问题.在下面的例子中,我需要这个结构位于单个字节的内存中(按照无线电标准),但它目前给我2个字节的大小.经过多次挖掘后,我明白这是因为联盟的"大小"是字节而不是3位......但是还没有解决这个问题.我看过:

但似乎都没有给我一个解决方案.

有任何想法吗?

谢谢!

#ifdef WIN32
#pragma pack(push)
#pragma pack(1)
#endif

typedef struct three_bit_struct
{
    unsigned char bit_a : 1;
    unsigned char bit_b : 1;
    unsigned char bit_c : 1;
}three_bit_struct_T;


typedef union
{
    three_bit_struct_T three_bit_struct;
    unsigned char another_three_bits : 3;
}weird_union_T;


typedef struct
{
    weird_union_T problem_union; 
    unsigned char another_bit : 1;
    unsigned char reserved : 4;
}my_structure_T;


int _tmain(int argc, _TCHAR* argv[])
{
     int size;

     size = sizeof(my_structure_T);

     return 0;
}


#ifdef WIN32
#pragma pack(pop) …
Run Code Online (Sandbox Code Playgroud)

c struct unions bit-fields

3
推荐指数
1
解决办法
1234
查看次数

标签 统计

bit-fields ×1

c ×1

struct ×1

unions ×1