Re: Help needed in dereferencing a union



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
.



Relevant Pages

  • syntax for fixed array in C#?
    ... the compiler complains that it cannot convert a byte field to a byte array. ... the compiler is happy, but I get a runtime exception complaining that A cannot be marshaled. ... I suspect there is some simple casting syntax where I can tell the compiler that, yes, A is a byte array, but the 4000 variations I have tried have failed. ...
    (microsoft.public.dotnet.general)
  • Re: Indirect visibility of private part in child packages
    ... package body P1.P3 is ... complains there are not selector Low_Level_Data for T1_Type. ... Converting to T1_Type should work. ... think that the compiler gonna complain that Set is a primitive of ...
    (comp.lang.ada)
  • Re: Difficult example?
    ... I used an old Sun Fortran 90 compiler. ... It complains about the ...
    (comp.lang.fortran)
  • VS2005 doesnt like its own suggestions
    ... A pull down list box poped up with a list of all selections available as ... The compiler then complains that "string is not a member of std"... ...
    (microsoft.public.vc.language)
  • Re: Pointers to char pointers in functions
    ... cast's for malloc unless the compiler complains (I generally use MS's ... Never add a cast for the sole purpose of inhibiting a compiler ...
    (comp.lang.c)