Bitfelder innerhalb eines Struct

Gast #1457276
Lesenswert?

Hallo,
ich würde gerne ein Struct definieren, welches zwei Bytes mit Bitfeldern 
enthält. Leider funktioniert folgender Code nicht:
1
typedef struct my_struct
2
{
3
  typedef struct
4
  {
5
    char byte_1_bit_1_4 : 4,
6
         byte_1_bit_5_8 : 4;
7
  } byte_1;
8

9
  typedef struct
10
  {
11
    char byte_2_bit_1_6 : 6,
12
         byte_2_bit_7_8 : 2;
13
  } byte_2;
14
} my_struct;
15

16
my_struct test;
17
test.byte_2.byte_1_bit_1_4 = 12;

Danke im Voraus!
#1457289
Lesenswert?

schon mal so probiert?
1
typedef struct my_struct
2
{
3
  struct
4
  {
5
    char byte_1_bit_1_4 : 4,
6
         byte_1_bit_5_8 : 4;
7
  } byte_1;
8

9
  struct
10
  {
11
    char byte_2_bit_1_6 : 6,
12
         byte_2_bit_7_8 : 2;
13
  } byte_2;
14
} my_struct;
15

16
my_struct test;
17
test.byte_1.byte_1_bit_1_4 = 12;

Edit: beim Test byte_2 zu byte_1 geändert...
#1457294
Lesenswert?

das kompiliert bei mir:
1
typedef struct my_struct
2
{
3
  struct
4
  {
5
    char byte_1_bit_1_4 : 4,
6
         byte_1_bit_5_8 : 4;
7
  } byte_1;
8

9
  struct
10
  {
11
    char byte_2_bit_1_6 : 6,
12
         byte_2_bit_7_8 : 2;
13
  } byte_2;
14
} my_struct;
15

16

17
int main( int nargs, char **args )
18
{
19
  my_struct test;
20
  test.byte_1.byte_1_bit_1_4 = 12;
21

22
  return 0;
23
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren