-->

Learn to code the on-demand coding Languages in an easy way. We help you understand programming concepts and coding Techniques Better.

Monday, February 8, 2021

What is Union in C | Difference between structure & union in C

 Union in C:


Union: Like structure, Union in C is a user-defined data type used to store different types of variables. In structure, each data member has its own memory location whereas in a union the variable having the size of the largest memory location is shared to all the members in the union.

Union Syntax in C: The union keyword is used to define the union.

    union Data
    {
        data_type member1;
        data_type member1;
        data_type member1;
        .
         .
         .
     }data;

The keyword union is a reserved word used to define the union

Variable data is used to assess the members of the union.

Union Declaration  in C: The Declaration of Union as follows

    union Data
    {
        int id;
        
        char name[10];

        float salary;
        
     }data;

The following image shows how the compiler will allocate the memory for the Data unionD.

    The total memory allocated for union Data is 10 bytes. The size of the union is the size of the largest element in the union. The largest element will occupy the memory and also shares the same memory to other members in the union.


Accessing Union Members:  There are two ways to access the members of the union.

    1. By using .(dot operator) 
    
    2. By using ->(structure pointer operator)

    dot(.) Operator: The dot operator is used to access the data members of the union using variables. It acts as an interface between variables and data members of the union.

     Example: Let's see a simple example for accessing data members of union using dot(.) operator.

     Program: 

  

    Output:



  Explanation: Int the above example, data is the union variable to access the union members by dot(.) operator. The memory allocated for the union is the size of the largest member(name). And the memory is shared to all other members in the union.


 pointer(->) Operator: Also called Arrow Operator. It is used to access the data members of the union using pointers.

    Program:


    Output:


  Explanation: Int the above example, *data is the union pointer variable to access the union members by Arrow(->) operator. The memory allocated for the union may vary depends on the variable and types of members in the union.


Difference Between Structure and Union:

                Structure

                   Union

The keyword struct is used to define a structure.

The keyword union is used to define a union.

When a variable is declared for a structure, the compiler will allocate memory for each member in the structure.

When a variable is declared for a union, the compiler will allocate the memory considering the size of the largest memory.

Each member within a structure is assigned a unique storage area.

Memory allocated is shared by individual members of the union.

Several members of the structure is initialized at once.

Only one member is initialized at once.

malloc() function is used for dynamic memory allocation when using -> operator.

malloc() function is used for dynamic memory allocation when using -> operator.



In this tutorial, you have learned about C union and also the difference between structure and union.


Prev<---Structures in C                                        Next Topic--->Enum in C

No comments:

Post a Comment