How to initialize a union?

Discussion and support of the compiler from ImageCraft for the M8C.

Moderator: richard@imagecraft.com

How to initialize a union?

Postby Who Knew » Sat Jan 26, 2013 9:21 am

Hi Folks,

I don't use unions and structures very much. I've got this one union that's global and I'd like to initialize it at program start, but I'm getting errors. Here's the union:

Code: Select all
   union
      {
         // keeps count of seconds that have elapsed since 12:00 a.m., Friday, January 1, 1904, Universal Time [01-01-1904 00:00:00].
         volatile unsigned long ulSeconds = 0;         
         // Lets me transfer above ulSeconds into a U8 array for TXing.
         volatile unsigned char ucSeconds [4];
      }   RTC;



If I understand correctly, I'm supposed to be able to initialize the first member of a union. However, I get

Code: Select all
 syntax error; found `=' expecting `;'



Thanks!
Who Knew
Cheese Wheel
Cheese Wheel
 
Posts: 145
Joined: Thu May 17, 2007 8:12 pm
Location: California, USA

Re: How to initialize a union?

Postby bobmarlowe » Sat Jan 26, 2013 9:38 am

Try this here...
Code: Select all
  union
      {
         // keeps count of seconds that have elapsed since 12:00 a.m., Friday, January 1, 1904, Universal Time [01-01-1904 00:00:00].
         volatile unsigned long ulSeconds;         
         // Lets me transfer above ulSeconds into a U8 array for TXing.
         volatile unsigned char ucSeconds [4];
      }   RTC  = {0};


That'll do the job

Happy coding
Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1490
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: How to initialize a union?

Postby Who Knew » Sat Jan 26, 2013 9:51 am

Thanks! :wink:
Who Knew
Cheese Wheel
Cheese Wheel
 
Posts: 145
Joined: Thu May 17, 2007 8:12 pm
Location: California, USA

Re: How to initialize a union?

Postby Who Knew » Sat Jan 26, 2013 11:55 am

Hi Bob, All,

I got it to work with some coaching from the compiler

this:

Code: Select all

   union
      {
         // keeps count of seconds that have elapsed since 12:00 a.m., Friday, January 1, 1904, Universal Time [01-01-1904 00:00:00].
         volatile unsigned long ulSeconds;         
         // Lets me transfer above ulSeconds into a U8 array for TXing.
         volatile unsigned char ucSeconds [4];
      }   RTC = 0;




made the compiler say

Code: Select all

 missing { in initialization of `union





It did seem to like this:

Code: Select all
   
   union
      {
         // keeps count of seconds that have elapsed since 12:00 a.m., Friday, January 1, 1904, Universal Time [01-01-1904 00:00:00].
         volatile unsigned long ulSeconds;         
         // Lets me transfer above ulSeconds into a U8 array for TXing.
         volatile unsigned char ucSeconds [4];
      }   RTC = {0};
   


So I thought {0} was just shorthand for initializing the 4-byte array, so I tried this

Code: Select all
   
   union
      {
         // keeps count of seconds that have elapsed since 12:00 a.m., Friday, January 1, 1904, Universal Time [01-01-1904 00:00:00].
         volatile unsigned long ulSeconds;         
         // Lets me transfer above ulSeconds into a U8 array for TXing.
         volatile unsigned char ucSeconds [4];
      }   RTC = {0, 0, 0, 0};
   


to which it said

Code: Select all

syntax error; found `0' expecting `}'
 skipping `0' `,' `0' `,' `0'



So seems it only likes {0}

Anyway, thanks just the same for the help. I thought I'd share my results in case it helps others along

Who
Who Knew
Cheese Wheel
Cheese Wheel
 
Posts: 145
Joined: Thu May 17, 2007 8:12 pm
Location: California, USA

Re: How to initialize a union?

Postby Who Knew » Sat Jan 26, 2013 12:01 pm

Sorry to beat a dead horse...

It appears that whatever you put between the brackets, initializes the first member of the union. So, if you want to init the 4-byte array values instead of the ul value, this also works

Code: Select all

   union
      {
         // Lets me transfer above ulSeconds into a U8 array for TXing.
         volatile unsigned char ucSeconds [4];      
         // keeps count of seconds that have elapsed since 12:00 a.m., Friday, January 1, 1904, Universal Time [01-01-1904 00:00:00].
         volatile unsigned long ulSeconds;         

      }   RTC = {0, 0, 0, 0};





Who
Who Knew
Cheese Wheel
Cheese Wheel
 
Posts: 145
Joined: Thu May 17, 2007 8:12 pm
Location: California, USA

Re: How to initialize a union?

Postby bobmarlowe » Sat Jan 26, 2013 12:42 pm

Yes, only the first member of the union can be initialized.
Here is a link to my favourite C-handbook use at your own risk :mrgreen:

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1490
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany


Return to “%s” ImageCraft ICCv7 for M8C Compiler

Who is online

Users browsing this forum: No registered users and 1 guest