Re: ANSI C: accesso ai dati di una union
|
- Subject: Re: ANSI C: accesso ai dati di una union
- From: *FeDe*
- Data: Tue, 1 Jul 2008 08:38:26 -0700 (PDT)
- Newsgroups: it.hobby.elettronica.digitale
> l'errore nel fatto che byte1 e' un 'campo' di tipo "struct Bits" e non
> un intero e che quindi non puoi assegnagli tout-court un valore intero
E' vero .. il problema è proprio quello!
Ho risolto come hai detto tu, dichiarando anche 4 byte (vedi in fondo
al messaggio)
Il risultato mi piace poco, perchè per accedere ai bit devo fare:
uAddr.byte1.bit0 = 1;
mentre per accedere ai byte devo fare:
uAddr.B1 = 45 ;
quando mi piacerebbe scrivere:
uAddr.byte1 = 45;
come potrei fare? forse dovrei mettere una union al posto della struct
dei bit .. ? boh ?!?
grazie per l'aiuto!
struttura:
union mnglng {
/* bit access */
struct Bits {
unsigned char bit0:1;
unsigned char bit1:1;
unsigned char bit2:1;
unsigned char bit3:1;
unsigned char bit4:1;
unsigned char bit5:1;
unsigned char bit6:1;
unsigned char bit7:1;
} byte1, byte2, byte3, byte4;
/* byte access */
unsigned char B1;
unsigned char B2;
unsigned char B3;
unsigned char B4;
/* long access */
unsigned long long1;
} uAddr,