Forum: Mikrocontroller und Digitale Elektronik Bitfelder innerhalb eines Struct


von Martin (Gast)


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!

von Klaus W. (mfgkw)


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...

von Martin (Gast)


Lesenswert?

Kompiliert leider auch nicht. :-(

von Klaus W. (mfgkw)


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
}

von Klaus W. (mfgkw)


Lesenswert?

Martin schrieb:
> Kompiliert leider auch nicht. :-(

das ist ja auch keine brauchbare Fehlerbeschreibung.
Was erwartest du jetzt? Daß jemand vorbei kommt und nachsieht?

von Martin (Gast)


Lesenswert?

Vielen Dank!

Es hatte sich nur ein dummer kleiner Fehler bezüglich Groß- und 
Kleinschreibung eingeschlichen...

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.