avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Problem with union


From: Eric Fu
Subject: Re: [avr-gcc-list] Problem with union
Date: Tue, 24 Aug 2004 08:15:34 +1000

Ned Konz Wrote:

> Probably used #define for the definition of 'word' , instead of a typedef
like
> you should have done.
>
> Using the standard typedefs, your code compiles just fine:
>
> // avr-gcc -mmcu=atmega16  -Wall xx2.c
>
> #include <inttypes.h>
> #include <avr/io.h>
>
> typedef union TwoBytes
> {                               /* unsigned 16 bit type for 8 & 16 */
>     uint16_t wForm;             /* bit accesses: 16 bit var.wForm    */
>     struct
>     {                           /* 8 bit var.byForm.High/Low          */
>         uint8_t High;
>         uint8_t Low;
>     } byForm;
> } TwoBytes;
>
>
> int main(int argc, char const * const * argv)
> {
>     TwoBytes ThisNodeID;
>
>     ThisNodeID.wForm = 1;
>
>     return 0;
> }
>
> -- 
> Ned Konz
> http://bike-nomad.com
>

Hi Ned,

I did "typedef unsigned short word;" in another header file which was
included. Your suggestion prompted me to put
"  ThisNodeID.wForm = 1; " inside a function. (I previously initialised
ThisNodeID.wForm outside a function). And now it works fine.
However, I initialised an unsigned char when I defined it outside any
functions and the compiler doesn't complain and it seems working fine. DO I
have to initialise a union inside a function only, while I can do it outside
any function for other global variables?

Thanks for help.

Eric Fu




reply via email to

[Prev in Thread] Current Thread [Next in Thread]