-
struct vs. union
When I was learning about structs and unions I understood the differences just fine...except for one part. My book never says if you need to do anything special when using unions to make sure you're only using one of the variables at once. I can only think of a few looping situations, or linked lists where one would have to even worry about it.
-
sometimes you would use unions using more than 1 type at a time. Other times not.
Things like extracting the sign, mantissa and exponent from a float. Depends on how the programmer wants to use a union.
-
>My book never says if you need to do anything special when using unions to make sure you're only using one of the variables at once.
The most common way of doing this is something like so:
Code:
struct fooby {
enum { INT_TYPE, DBL_TYPE, CHAR_TYPE } type;
union {
double d;
int i;
char c;
};
};