I have a doubt regarding unions. Suppose there is a union defined as
union emp{
float f;
int i;
};
Is there a way to determine which field of union is used. I have come across a situation in which I have to print float if float field is used and print int if int field is used.The function may be like
void print(union u){
// if float field is initialized
printf("float field = %f\n",u.f);
// if int field is initialized
printf("int field = %d\n",u.i);
}
Thanks in advance.
You can’t tell with just the
union; typically you wrap it in astructwhich includes a tag value of some kind.