Unions. Initialize and Declare Unions, C Programming Assignment Help

Assignment Help: >> Structure and Unions >> Unions. Initialize and Declare Unions, C Programming

Unions

Unions are derived data type the way structures are. Union and structure look alike, but are engaged in totally different. Both structure and union are used to group a number of different variables together. But while a structure enables us to treat a number of different variables stored at different place in memory. A union enables us to treat the same space in memory as a number of different variables. That is a union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion. Example:

main( )

{

                union a

                {

                                int x;

                                char ch[2];

                };

                union a uni;

                uni.x = 512;

                printf("uni.x = %d\n",uni.x);

                printf("uni.ch[0] = %d\n",uni.ch[0]);

                printf("uni.ch[1] = %d\n",uni.ch[1]);

}

Output:

uni.x = 512

uni.ch[0] = 0

uni.ch[1] = 2

 Declaration of a data type union and its variable is similar to declare structure data type and its variable. Also the union elements are accessed exactly the same way in which the structure elements are accessed, using '.' Operator.

                union a                                                                 struct a

                {                                                                              {

                                int x;                                                                      int x;

                                char ch[2];                                                           char ch[2];

                };                                                                             };

However, the similarity ends here. In these examples the structure would occupy 4 bytes in memory, 2 for uni.x and one each for uni.ch[0] and uni.ch[1], as shown above. But the data type union is occupies only 2 bytes in memory. Note that same memory locations which are used for uni.x are also being used by uni.ch[0] and uni.ch[1]. It means that the memory locations used by uni.x can also be accessed using uni.ch[0] and uni.ch[1].

Union is a concept borrowed from structures and therefore follows the same syntax as structures. However, there is major distinction between them in terms of storage. In structures, each member has its own storage location, whereas all the members of a union use the same location. This implies that, although a union may contain many members of different types. It can handle only one member at a time. Like structures, a union can be declared using the keyword union as follows:

union item

{

                    int m;

                    float x;

                    char c;

} code;

The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union. In the declaration above, the member x requires 4 bytes which is the largest among the members. To access a union member, we can use the same syntax that we use for structure members. That is

code.m

code.x

code.c

During accessing, we should make sure that we are accessing the member whose value is currently stored. For example, the statements such as

code.m=379;

code.x=7867.09;

printf("%d",code.m);

Would give garbage value.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd