Initializing a union

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello boys & girls
I want to initialize a union that I use for holding a lot of flags. This is the union:

union LCD_ConfigFlagsType
{
	unsigned int all;						// 16 flags
	struct
	{
		unsigned char Interface4Bits:1;
		unsigned char IncreaseCursor:1;	
		// ... and a few more...
		unsigned char	UnusedBits:4;
	};
};

For initialization:

union LCD_ConfigFlagsType	LCD_ConfigFlags_eemirror	__attribute__ ((section (".eeprom"))) =
{
	//.all	= 0,
	.Interface4Bits		= 1,
	.IncreaseCursor		= 1,
	// ... and all the other...
};

The compiler complains with this message:
unknown field `Interface4Bits' specified in initializer
for each flag I try to initialize. I searched the forum, and found a post on this issue, where C99 is mentioned as solution. But I'm already using flag gnu99! :(
I'm actually not sure if syntax is correct, I'm not that familiar with union.

Ideas, anyone?

/Jakob Selbing

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Shouldn't you name the stuct "bits" or something and then set the initializers to be .bits.Interface4Bits = 1 etc. ?

Don't know but normally each element in a union would be named. Equally, in code I dont thing you could delcare a variable "fred" of typ LCD_ConfigFlagsType and then access fred.Interface4Bits - you'd need to name the struct and use fred.bits.Interface4Bits

Or am I wrong?

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Cliff
Thanks, that was it. I did actually suspect that, but in fact I copied code from one of Atmel's app notes, which does compile fine. In that code, the bits are accessed the same way as in my initialization, i.e. nothing like "bits" used for the struct. But perhaps this is a case where the compiler does something smart instead...

/Jakob Selbing