Disclaimer: The following is trying to simplify the problem as much as possible. Originally the variable int x
is a struct but I would figure that this is not important here.
Assume we have two structs in a union (I have no influence on this)
typedef struct a_t {
int x;
int irrelevant;
} a_;
typedef struct b_ {
float also_irrelevant;
int x;
} b_;
typedef union uni_t{
a_ a;
b_ b;
} uni;
Is it possible to access x
in both structures by the same statement like ptr_to_struct->x
? But afaik the pointer needs the correct type at compile time. So a dependend declaration similar to this Pseudocode
if (union contains a_)
{
a_ * ptr_to_struct; // but outside of this scope
ptr_to_struct = &(uni.a);
}
else
{
b_ * ptr_to_struct; // but outside of this scope
ptr_to_struct = &(uni.b);
}
is impossible as far as I know.
Are there possibilties to get a "general" access to the variable x
independent of the current state of the union?