简体   繁体   English

C中的位域结构内的位域结构

[英]Bit-field struct within bit-field struct in C

I'm trying to find another way to create a bit-field structure within a bit-field structure in C. 我正在尝试寻找另一种在C中的位域结构内创建位域结构的方法。

Somewhat like this: 有点像这样:

typedef struct
{
  int A : 16;
  int B : 16;
} Struct1;

typedef struct
{
  int A     : 16;
  Struct1 B : 32;
} Struct2;

But the C Compiler doesn't like it, and it has to be bit-field. 但是C编译器不喜欢它,它必须是位域的。 A friend came up with using unions, but was wondering if someone knows another method besides using unions for this? 一位朋友想出了使用联合的方法,但想知道是否有人除了使用联合以外还知道另一种方法?

Thanks! 谢谢!

If I do this: 如果我这样做:

typedef struct
{
  int A : 16;
  int B : 16;
} Struct1;

typedef struct
{
  int A     : 16;
  Struct1 B;
} Struct2;

then 然后

Struct2 abc;

abc.A = 0x1111;

abc.B.A = 0x1123;

abc.B.B = 0x3334;

accepts assignments and can be used like bitfields. 接受分配,可以像位字段一样使用。

This might be of assistance to you. 可能对您有帮助。 Ofcourse you have to change the declarations a bit. 当然,您必须稍微更改声明。

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

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