0

I'm trying to find another way to create a bit-field structure within a bit-field structure in 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. A friend came up with using unions, but was wondering if someone knows another method besides using unions for this?

Thanks!

Skynight
  • 507
  • 2
  • 7
  • 24

2 Answers2

1

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.

ryyker
  • 22,849
  • 3
  • 43
  • 87
0

This might be of assistance to you. Ofcourse you have to change the declarations a bit.

Community
  • 1
  • 1
shunyo
  • 1,277
  • 15
  • 32
  • Thanks, Ryyker's comment seemed to do the trick! Just remove the bit-field declaration on Struct1, still needs some testing but we'll see. – Skynight Sep 28 '13 at 23:02