unionsa union is also like a structure except


Unions

A union is also like a structure, except that only single variable in the union is stored in the allocated memory at a time. It is a collection of mutually exclusive variables, which means all of its member variables share the similar physical storage and only one variable is explained at a time. The size of the union is equal to the largest member variables. A union is defined as follows :

                union tag

                 {

                                type memvar1;

                                type memvar2;

                                type memvar3;

                                :

                                :

                 };

 

A union variable of this data type can be declared as follows,

Union tag variable_name;

e.g.

                union utag

                 {

                                int num;

                                char ch;

                 };

                union tag filed;

The above union will have two bytes of storage allocated to it. The variable num can be accessed as field.sum and ch is accessed as field.ch. At any time, only one of these two variables,  can be referred to. Any change made to single variable affects another.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: unionsa union is also like a structure except
Reference No:- TGS0309314

Expected delivery within 24 Hours