Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unions within unions

Tags:

c

unions

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?

like image 410
Raj Avatar asked Sep 29 '10 15:09

Raj


People also ask

Can you have a union within a union?

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.

What were the two types of unions?

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.

What are unions called?

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.


1 Answers

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.

like image 167
R.. GitHub STOP HELPING ICE Avatar answered Oct 13 '22 01:10

R.. GitHub STOP HELPING ICE