-->

Facebook

Jasper Roberts - Blog

Wednesday, April 15, 2015

Difference between Structure and Union

Structure
Union
1. The keyword  struct is used to define a structure
1. The keyword  union is used to define a structure
2. In structure, each member has its own separate space.
2. In union, all members share the storage space.
3. Memory occupied is the total required to store all the members.
3. Memory occupied is only that much which is required to store the largest member of union.
4. All members can be accessed at anytime.                                                      
4. Only one member can be activated at a time.
5. Syntaxt:
struct strcture_name
      {
         datatype-1 variable-1;
         datatype-2 variable-2;
         datatype-3 variable-3;
           }

5. Syntaxt:
union strcture_name
      {
         datatype-1 variable-1;
         datatype-2 variable-2;
         datatype-3 variable-3;
    }
6. Example:
struct std
      {
          int rollno;
          char name[20];
          float salary;
      }
Total Size required  = 2 + 20+ 4
                                 = 26
6. Example:
union std
      {
          int rollno;
          char name[20];
          float salary;
      }
Total Size required  = max( 2, 20, 4)
                                 = 20

No comments:

Post a Comment