Union Struct Fehler

Gast #3301180
Lesenswert?

Liebe Kollegen!

Kann mir jemand sagen wo der Fehler liegt?
1
union fnc_lat
2
{
3
  struct {
4
  unsigned int cb      :2;    // Control Bit
5
  unsigned int cr      :1;    // Counter Reset
6
  unsigned int pd1    :1;    // Power Down 1
7
  unsigned int mux    :3;    // MUX Control
8
  unsigned int pcp    :1;    // PD Polarity
9
  unsigned int cdt    :1;    // CP Threestate
10
  unsigned int fe      :1;    // Fastlock Enable
11
  unsigned int fm      :1;   // Fastlock Mode
12
  unsigned int tcc    :4;    // Timer Counter Control
13
  unsigned int cs1    :3;   // Current Setting 1
14
  unsigned int cs2    :3;    // Current Setting 2
15
  unsigned int pd2    :1;    // Power Down 2
16
  unsigned int pv      :2;    // Prescaler Value
17
} BITS;
18
  uint32_t RAW;
19
}
20

21
//Init
22

23
union fnc_lat i_lat_init = {3,0,0,0,0,0,0,0,0,0,0,0,0};
24

25

26
union fnc_lat i_lat_save = i_lat_init; //FEHLER: expression must have a constant value
Gast #3301266
Lesenswert?

Gerhard schrieb:
> } BITS;
>   uint32_t RAW;
> }

zu

  uint32_t RAW;
}bob;

semi, keine anonyme Union

>
> //Init
>
> union fnc_lat i_lat_init = {3,0,0,0,0,0,0,0,0,0,0,0,0};
>

union fnc_lat i_lat_init = {{3,0,0,0,0,0,0,0,0,0,0,0,0}};


> union fnc_lat i_lat_save = i_lat_init; //FEHLER: expression must have a
> constant value

funktioniert.

i_lat_save.BITS.cb hat den Wert 3.
Gast #3301277
Lesenswert?

wenn ich const union ... schreibe geht es auch nicht.
Darf ich nur Werte nehmen??
1
union fnc_lat
2
{
3
  struct {
4
  unsigned int cb      :2;    // Control Bit
5
  unsigned int cr      :1;    // Counter Reset
6
  unsigned int pd1    :1;    // Power Down 1
7
  unsigned int mux    :3;    // MUX Control
8
  unsigned int pcp    :1;    // PD Polarity
9
  unsigned int cdt    :1;    // CP Threestate
10
  unsigned int fe      :1;    // Fastlock Enable
11
  unsigned int fm      :1;   // Fastlock Mode
12
  unsigned int tcc    :4;    // Timer Counter Control
13
  unsigned int cs1    :3;   // Current Setting 1
14
  unsigned int cs2    :3;    // Current Setting 2
15
  unsigned int pd2    :1;    // Power Down 2
16
  unsigned int pv      :2;    // Prescaler Value
17
} BITS;
18
  uint32_t RAW;
19
}
20

21
//Init
22

23
const union fnc_lat i_lat_init = {3,0,0,0,0,0,0,0,0,0,0,0,0};
24

25

26
union fnc_lat i_lat_save = i_lat_init; //FEHLER: expression must have a constant value
Gast #3301287
Lesenswert?

Gerhard schrieb:
> } BITS;
>   uint32_t RAW;
> };
1. fehlt da bei dir das Semikolon
> const union fnc_lat i_lat_init = {{3,0,0,0,0,0,0,0,0,0,0,0,0}};
2. Müssen da doppelte {{ und }} hin

3.: Kompiliere einfach mit C++, da geht sowas. Schreibe dann auch 
"constexpr" vor die letzte Zeile um sicherzustellen, dass die "statisch" 
initialisiert wird.
Gast #3301291
Lesenswert?

Das fehlende Semikolon ist beim kopieren verschwunden. Im Code steht es.
Cool, dass dir das aufgefallen ist.

Ich habe es so gelöst:
1
union fnc_lat
2
{
3
  struct {
4
  unsigned int cb      :2;    // Control Bit
5
  unsigned int cr      :1;    // Counter Reset
6
  unsigned int pd1    :1;    // Power Down 1
7
  unsigned int mux    :3;    // MUX Control
8
  unsigned int pcp    :1;    // PD Polarity
9
  unsigned int cdt    :1;    // CP Threestate
10
  unsigned int fe      :1;    // Fastlock Enable
11
  unsigned int fm      :1;   // Fastlock Mode
12
  unsigned int tcc    :4;    // Timer Counter Control
13
  unsigned int cs1    :3;   // Current Setting 1
14
  unsigned int cs2    :3;    // Current Setting 2
15
  unsigned int pd2    :1;    // Power Down 2
16
  unsigned int pv      :2;    // Prescaler Value
17
} BITS;
18
  uint32_t RAW;
19
} ;
20

21
//Init
22

23

24
#define init_i{{3,0,0,0,0,0,0,0,0,0,0,0,0}}
25

26
const union fnc_lat i_lat_init = init_i;
27

28
union fnc_lat i_lat_save = init_i;

DANKE! für Eure Hilfe!

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