CW4.5 S12X Initializing unions

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CW4.5 S12X Initializing unions

2,378 Views
dp
Contributor III

Is there a way in CW to control type when initializing union members?

In the code below, all the param elements are initialized as integers even though some are ints and some are floats. If I change the declaration so float appears first in the union, all params are initialized as floats.

I've tried typecasting the assigned values but that doesn't work. Any ideas?

Dave
typedef struct {  char *Desc;    union {    int i;    float f;  } Val;} TestStruct;const struct {  char *Desc;  TestStruct Param[2];} List[] = {    {"ListItem1",       "param1", 1,       "param2", 2.0},       {"ListItem2",      "param1", 3.0,      "param2", 4}  };void main(void) {  for(;;) {}}
Labels (1)
Tags (1)
0 Kudos
6 Replies

592 Views
Navidad
Contributor III
This is exactly what the standard states about initializing unions: when using an initializer for a union, only values of the same type with the first member of the union are taken into account. For instance:

union myUnion { int i; char* s; };

union myUnion  u = { 1 };  // ok

union myUnion u1 = { "c"};  // not ok

In the second case you will get an error.

Unfortunately you may have to programmatically initialize each member of your array.

0 Kudos

592 Views
dp
Contributor III
Since the array is a const in flash I cannot change values with a simple assignement. I was hoping someone  knew of a way to do this specifically in CW.
0 Kudos

592 Views
Lundin
Senior Contributor IV
If the values are in the flash, ie they are const, why would it matter which member you initialize? You are obviously going to use all members of the union, otherwise you wouldn't have picked that data type. Simply place the union member you wish to initialize first.
0 Kudos

592 Views
dp
Contributor III
Even though these are consts in flash, the struct defines a format used in an array. Some of the parameters of that struct will be floats, some ints even though they may occupy the same relative position in the the struct. A union would be the perfect way to handle both types until I ran into the initialization problems.

Navidad is correct about the c rules regarding union initializations. Whatever the compiler encounters first will be the type used for the remaining assigments just as in my example above.

The solution of getting rid of the union and declaring floats works fine. I use another element of the struct to tell me whether the value needs to be used as a float or int then re-cast to int as needed in the program.
0 Kudos

592 Views
Lundin
Senior Contributor IV
I still don't see any problem here...

typedef union
{
struct
{
int someInt;
float someFloat;

/* add more members, nested structs, arrays... */

};

SomeOtherStructure a;
SomeVeryComplicatedStructure b;
unsigned char bytes[x];
/* add as many as you like... /*

} SomeUnion;


const SomeUnion myUnion = {123, 3.14, /*...*/ };
0 Kudos

592 Views
dp
Contributor III
I'll get rid of the union, declare and initialize as a float, and re-cast to an int in the code when needed.
0 Kudos