In C, is it possible to define a union within another union? If no, why is it not possible? Or if yes, where can it be used?
Yes, a union may contain another union: union foo { int x; double y; union bar { char blah[10]; char *blurga; } bletch; }; I can't think of a situation where it would be useful (or even desirable), though. It is commonly used in the embedded world.
There are two types of unions: the horizontal union, in which all members share a common skill, and the vertical union, composed of workers from across the same industry.
Officially known as a “labor organization,” and also called a “trade union” or a “workers union,” a labor union selects representatives to negotiate with employers in a process known as collective bargaining.
Suppose you want to define:
union myun {
int x;
sometype y;
};
where sometype
is a typedef
defined by a library you're using. If the library happened to implement it as a union type, then this would be a union within a union, and it would make sense because you can't (from a good design standpoint) violate the encapsulation of the library's type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With