It is true that you cannot do such things out of the box.
A common way around this is that you can add a type along with your union. For example, it could be:
enum { charArr_type, float_type, int_type } VALUE_TYPE; typedef union { char charArr[SIZE]; int intVal; float floatVal; } VALUE; struct my_value { VALUE val, VALUE_TYPE val_type }
After that, you just need to update the type when assigning your structure:
my_value number; number.val.intVal = 8; number.val.val_type = is_int
This is an ordinary modern template when you need to have a common type that can store almost everything.
For example, you can find it everywhere in the PHP source code. So they store different types of values ββin the same object. See this page for more details.
source share