Re: Help needed in dereferencing a union
- From: Jens.Toerring@xxxxxxxxxxxxxxxxxxx
- Date: 31 May 2005 20:08:28 GMT
Jirka Klaue <jklaue@xxxxxxxxxxxxxxxx> wrote:
> On Tue, 31 May 2005 19:25:27 +0200, jlara <jlara@xxxxxxxxxx> wrote:
>> typedef union
>> {
>> uint8 byte;
>> struct
>> {
>> uint8 : 4;
> uint8 bla : 4;
>> uint8 tffca : 1;
>> uint8 tsfrz : 1;
>> uint8 tswai : 1;
>> uint8 ten : 1; /* timer enable */
>> }bit;
>> }tSCR1;
>>
>> #define scr1 (*(tSCR1 *)(0x4c))
>> scr1.bit.ten = 1; /* the compiler complains about this */
> No, it complains about a missing field name in bit.
It's not the missing field name my compiler (gcc) is complaining
about (unnamed bit-fields being allowed by the standard) but the
type of the variable used for the bit-field - "unit8" isn't a
standard type, it would have to "uint8_t" (assuming a C99 compli-
ant compiler) and then for bit-fields only signed and unsigned
ints can be used portably (and _Bool in C99) according to the
standard. With that out of the way it compiles without problems,
the line indicated by the OP getting accepted without complaints.
If the OPs compiler has a real problems with only that line (which
I guess shouldn't be the case) an alternative would be to try to
use a pointer to the union instead
#defined scr1 ( ( tSCR1 * ) 0x4c )
and then do
scr1->bit.ten = 1;
or to defined macros for setting an resetting the bits, i.e.
#define TIMER_ENABLE() do { ((tSCR1 *) 0x4c )->bit.ten = 1; } while(0)
#define TIMER_DISABLE() do { ((tSCR1 *) 0x4c )->bit.ten = 0; } while(0)
which, since this seems to be mostly about readability of the code,
could result in the easiest to read code.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@xxxxxxxxxxxxxxxxxxx
\__________________________ http://www.toerring.de
.
- References:
- Help needed in dereferencing a union
- From: jlara
- Re: Help needed in dereferencing a union
- From: Jirka Klaue
- Help needed in dereferencing a union
- Prev by Date: Re: Multiple return statements Vs goto's
- Next by Date: Re: Formatted IO; %d or %i?
- Previous by thread: Re: Help needed in dereferencing a union
- Next by thread: message buffering for logs, sprintf, etc...
- Index(es):
Relevant Pages
|