Union auf bestimmte struct Member anwenden?!

Gast #5336194
Lesenswert?

1
typedef struct
2
{
3
  uint8_t red;
4
  uint8_t green;
5
  uint8_t blue;
6
  
7
  uint8_t state;
8
}colors_t;

Hallo,

ist es möglich mit einem "union" die 3 x Member aus dem struct zu 
beeinflussen?

Ich möchte gerne mit einer Variable alle 3 aufeinmal herunter zählen.

Pseudocode..
1
colors_t color;
2
color.red = 10;
3
color.green = 10;
4
color.blue = 10;
5

6
for( uint8_t i = 0 ; i < 10 ; i++)
7
{
8
  color.union.alle_drei--;
9
}

Wie stellt man das mit einem Union an? Die andere Variable (state) im 
"struct" soll nicht beeinflusst werden.

Danke für Hilfe.
Gast #5336220
Lesenswert?

> Danke für Hilfe.

Das geht grundsätzlich. Dir sollte aber bewußt sein, dass es u.a. 
plattformabhängig ist. Es ist keine saubere Sache. Eine statische 
Codeanalyse würde explodieren vor Wut.
1
struct X
2
{
3
   uint8_t r;
4
   uint8_t g;
5
   uint8_t b;
6
   uint8_t s;
7
};
8

9
union Y
10
{
11
   uint32_t all;
12
   struct X members;
13
};
14

15
typedef union Y pixel;

Schleife Pseudo-Code:
1
pixel q;
2

3
q.members.r = 10;
4
q.members.g = 10;
5
q.members.b = 10;
6
for (...)
7
{
8
    q.all -= 0x10101u;
9
}

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