Set compilation value at compile time

Possible duplicate:
Is it possible to initialize the union in the declaration?

I have looked all over the Internet and cannot find an example of how to set the union value inside the structure at compile time, and I hope you guys and girls can help me. For example, a simple structure:

typedef enum { typeFloat, typeInt } Type; typedef struct myStruct { Type elementType; int valueInt; float valueFloat; } myStruct; 

and then you can declare a local variable with:

 myStruct structEx = {typeInt, 349, 0}; 

or

 myStruct structEx = {typeFloat, 0, 349.349}; 

How would you do the same if the structure was declared as:

 typedef struct myStruct { Type elementType; union value { int valueInt; float valueFloat; } value; } myStruct; 

The "value" will be either a float or an int with an "elementType" letting it know what it was.

I know that you can install it at runtime:

 myStruct structEx; structEx.elementType = typeInt; structEx.value.valueInt = 349; 

but I did not find a way to do this, as indicated above, using structure.

Thanks in advance.

Edit: this is a duplicate. I should have used the word initialization, and that would have led me to this. My Google-Fu should be weak today. Thanks.

+4
source share
1 answer

What about:

 myStruct structEx = { .elementType = 0, .value = { .valueInt = 42 } }; 

Or maybe,

 myStruct structEx = { .elementType = 0, .value.valueInt = 42 }; 
+3
source

All Articles