Difference between Structures and Unions

Discussion in 'C' started by johnson10001, Aug 12, 2014.

  1. johnson10001

    johnson10001 New Member

    Joined:
    Aug 12, 2014
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    JAVA Training Center in Chennai
    Location:
    Chennai
    Home Page:
    http://www.javatrainingchennai.co.in/
    Hi every one..I am new to programming..Can any one tell me the difference between structures and unions in C
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    There are quite a few difference between struct and union.
    • Structure has memory equal to sum of all member variables where as union has memory equal to hold the largest variable type of all the member variables.
    • In Structure all members variables can be used to store data and retrieve it where as in union only one member variable can be used.
    You can read more about unions here along with examples.
     
  3. bigdatatraining

    bigdatatraining New Member

    Joined:
    Dec 30, 2015
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Big Data Training Institute in Chennai
    Location:
    Chennai
    Home Page:
    http://www.traininginsholinganallur.in/big-data-analytics-training-in-chennai.html
    Hi..To know more details about the difference between structure and union, please refer this link.
    Structure
    Union
    1.The keyword struct is used to define a structure
    1. The keyword union is used to define a union.
    2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes.
    2. When a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.
    3. Each member within a structure is assigned unique storage area of location.
    3. Memory allocated is shared by individual members of union.
    4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values.
    4. The address is same for all the members of a union. This indicates that every member begins at the same offset value.
    5 Altering the value of a member will not affect other members of the structure.
    5. Altering the value of any of the member will alter other member values.
    6. Individual member can be accessed at a time
    6. Only one member can be accessed at a time.
    7. Several members of a structure can initialize at once.
    7. Only the first member of a union can be initialized.
     
  4. senthil_a4nlabs

    senthil_a4nlabs New Member

    Joined:
    Aug 27, 2016
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    • Structure has memory equal to sum of all member variables where as union has memory equal to hold the largest variable type of all the member variables.
     
  5. persysweb

    persysweb Member

    Joined:
    Aug 1, 2017
    Messages:
    98
    Likes Received:
    18
    Trophy Points:
    8
    Location:
    India
    Home Page:
    httpS://persys.in/
    Structure:
    A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record.
    Defining a structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows:
    Code:
       struct [structure name]
       {
           member definition;
           member definition;
           ...
           member definition;
       };
     
    Union:
    A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.
    Defining a Union: To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows:
    Code:
        union [union name]
        {
           member definition;
           member definition;
           ...
           member definition;
        };
    
    Thanks!
     
    shabbir likes this.
  6. Mridula0310

    Mridula0310 New Member

    Joined:
    Jan 31, 2018
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Structure and Union both are user defined data types but the major difference between both of them is that in structure each member gets separate space in memory whereas in Union the memory allocated is equal to the member with largest size.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice