简体   繁体   English

我们可以将位字段用于c中的结构成员的数组吗?

[英]Can we use bit field for an array which is member of the structure in c?

Can we use bit field of an array which is a part of the structure? 我们可以使用属于结构一部分的数组的位字段吗? Is this possible in C? 这在C中可能吗?

typedef struct convert
{
    unsigned char data[7] : 5;                                        //5x7
};

No you can not do that. 不,你不能那样做。

  • Bitfields can only be of type int 位域只能是int类型
  • Bitfields are padded to the size of the next element in the struct . 位域被填充为struct下一个元素的大小。

If your array is small enough then you may assign them individual names and make 7 5-bit bitfields, but that is probably not what you want. 如果数组足够小,则可以为它们分配单独的名称,并创建7个5位位域,但这可能不是您想要的。

Shifting them manually with a macro will be your best bet and as fast. 用宏手动移动它们将是您最好的选择,而且速度也一样快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM