Tagged Union
You could use a union:
typedef union
{
type1_t t1;
type2_t t2;
} UnionType;
You need to somehow know if t1
or t2
is active. For this you can pass an additional enumeration value (A distinct value for each type you want to allow):
enum types
{
TYPE1,
TYPE2
};
Combine it to a struct, and there you have your typex_t
:
typedef struct
{
enum types type; // this is also called a type tag
UnionType content;
} typex_t;
Or more compact (nonstandard)
typedef struct
{
enum { TYPE1, TYPE2 } type;
union { type1_t t1; type2_t t2; } content;
} typex_t;
This whole struct union
construct is also called a tagged union.