Difference Between Structure and Union


Difference Between Structure and Union


C++  permits all the five ways in which the c language provided to form a custom knowledge. Those  five ways that are structure  bit-field, union, enumeration, typedef  within the article below we have a tendency to are getting to study the distinction between structure and union. The structure and union each  are the instrumentality. Knowledge sorts that may hold knowledge of any type.

Definition of structure and union:

The structure may be a assortment of variables of various knowledge sorts, that square measure referred by a typical name. the variables in a very structure square measure known as members. By default , all the members of a structure square measure public.

When you declare a structure you produce a model, which may be accustomed produce structure objects,  sharing constant organization of information members and member functions. The declaration of the structure is often preceded by the keyword struct that tells the compiler that a structure has been declared.

A union may be a memory location shared by two or additional totally different form of variable declared underneath one union sort.  The keyword want to declare a union is union.  In c++, a union could contain each member operate and variables . by default, all the members of the union are public. Declaration of the union is analogous to the declaration of the structure.

 similarities of structure and union:

Both structure and union have an equivalent manner of declaring itself, making variables and therefore the same manner of accessing members of the variables. Both structure and union is passed to a operate by each the strategies decision  by worth and decision by reference. Both structure associate in nursing union  are instrumentality knowledge sort & contain an object of any knowledge sort, as well as different structure , union, array as their members.


Both, structure and union are the instrumentation data type that contains the member of various sorts. However structures are used once we have to be compelled to store distinct worth for all the members in an exceedingly distinct memory location. The unions are used once kind conversion are required.