20 Apr 2017

Difference Between Structure and Union


structure and unionC++ allows all the five ways that the C language provided to create a custom data. Those five ways are ‘structure’, ‘bit-field’, ‘union’, ‘enumeration’, ‘typedef’. In the article below we are going to study the difference between structure and union. The structure and union both are the container data types that can hold data of any “type”. The one major difference that distinguishes structure and union is that the structure has a separate memory location for each of its members whereas, the members of a union share the same memory location.
Let’s understand the difference between structure and union, along with a comparison chart.

Content: Structure Vs Union

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Similarities
  5. Conclusion

Comparison Chart

BASIS OF COMPARISONSTRUCTUREUNION
BasicThe separate memory location is allotted to each member of the 'structure'.All members of the 'union' share the same memory location.
Declarationstruct struct_name{
type element1;
type element2;
.
.
} variable1, variable2, ...;
union u_name{
type element1;
type element2;
.
.
} variable1, variable2, ...;
keyword'struct''union'
SizeSize of Structure= sum of size of all the data members.Size of Union=size of the largest members.
Store ValueStores distinct values for all the members.Stores same value for all the members.
At a TimeA 'structure' stores multiple values, of the different members, of the 'structure'.A 'union' stores a single value at a time for all members.
Way of ViewingProvide single way to view each memory location.Provide multiple way to to view same memory location.
Anonymous featureNo Anonymous feature.Anonymous union can be declared.

Definition of Structure

The structure is a collection of variables of different data types, which are referred by a common name. The variables in a structure are called “members”. By default, all the members of a structure are “public”.  When you declare a structure, you create a template, which can be used to create structure objects, sharing the same organization of data members and member functions. The declaration of the structure is always preceded by the keyword “struct”, which tells the compiler that a structure has been declared. Let’s take an example.
Let’s take an example.
  1. struct employee{
  2. string name;
  3. string company_name;
  4. string city;
  5. }emp1,emp2;
Here, we declare a structure for storing the information of the employees. The declaration is terminated by a semicolon, as structure declaration is a statement and, in C++ the statement is terminated by a semicolon.
The name given to a  structure defines a “type”(in above example the name of the structure is “employee”). The variables of these types could be created, as above, we created two variable ’emp1′ and ’emp2′ of type “employee”. In the above example, we created a ‘structure’ variable just after the declaration; alternately, it could be created separately.
  1. struct employee emp1, emp2; // the keyword 'struct' is not mandatory.
The members of a structure variable can be initialized or can be accessed by using the dot(.) operator.
  1. emp1.name="ashok";
The information contained in one structure variable can be assigned to another structure variable of the same type as follow.
  1. emp1.name="ashok";
  2. emp1.company_name= "Teckpix";
  3. emp1.city="Delhi";
  4. emp2=emp1; // assigning values of the member of 'emp1' to 'emp2'.
  5. cout<<emp2.name; //ashok
  6. cout<<emp2.company_name; //Teckpix
  7. cout<<emp2.city; //Delhi
Here, we assigned the structure variable ’emp1′ to ’emp2′ so, ’emp1′ copies all the value of its members to the corresponding member of ‘epm2’.
The member of the structure variable can be passed to the function.
  1. funct(emp1.city);
The entire structure variable can be passed to the function, by both the methods call by value and call by reference.
  1. funt(emp1); // calling function by 'call by value' method.
  2. .
  3. .
  4. void funct(struct employee emp){ // receving values of members of emp1.
  5. cout<<emp.city; //Delhi
  6. .
  7. }
Here, the change to the value of a member of the structure variable will not reflect outside the function as a variable is passed by call by value method. Now let’s do the same thing by call by reference method.
  1. funt(&emp1); // calling function by 'call by reference' method.
  2. .
  3. .
  4. void funct(struct employee *emp){ // receving address of emp1.
  5. emp->city="Nagpur"; // change the value of the member(city) of structure variable emp1.
  6. .
  7. }
Here, the structure variable is passed by reference so, change to the value of members of the structure variable also reflects outside the function also.
  1. srtuct employee *emp; //decalring the structure pointer of type 'employee'.
  2. emp=&emp1; // assigning the address of 'emp1' to the pointer.
  3. emp->city // pointer accessing the member 'city' of 'emp1'.
The pointer to the structure can also be created; it holds the address of structure variable.
In structure the aggregate initialization is allowed when the structure definition does not contain, any user defined constructor or virtual functions or base class or private or protected field.
  1. int main(){
  2. struct epm3={"Anil", "Teckpix", "Nagpur"};
  3. } // Possible as structure of employee does not contain any of above mention things.

Definition of Union

A union is a memory location shared by two or more different type of variable declared under a single union type. The keyword used to declare a union is “union”. In C++, a union may contain both member function and variables. By default, all the members of the union are “public”.  Declaration of the “union” is similar to the declaration of the structure.
  1. union u_type{
  2. int x,
  3. char c;
  4. float f;
  5. }u1, u2;
Here, we had declared a union named u_type.  The members of the u_type are ‘x’ of integer type, ‘c’ of character type and ‘f’ of float type. We had also created the union variables ‘u1’ and ‘u2’, of ‘u_type’ type, just after the declaration of a union. We can also declare the union variable separately from the declaration of a union.
  1. int main(){
  2. union u_type u1, u2; //The keyword union is not mandatory in C++.
  3. }
The accessing of members of the union can be done by using dot(.) operator, preceded by union variable and followed the member of that variable.
  1. u1.x=10;
Like structures, the aggregate initialization is not possible in a union. As we know the union shares the same memory location for all of its member at a time only one variable is initialized and all the variable automatically updated with the initialized value.
  1. u1.x=10;
  2. cout<<u1.c; //10
  3. cout<<u1.f; //10
If you try to change the value of any member of ‘u1’. The other member will be automatically updated to that value.
  1. u1.c=65;
  2. cout<<u1.x; //65
  3. cout<<u1.f; //65
The space allotted to the union is equal the size of the largest member of the union. As the byte allocated to ‘char’ is 1 byte, ‘int’ is 4 byte and ‘float’ is 4 bytes so, the largest size is 4 byte. So, the memory allotted to ‘u1’ and ‘u2’ is 4 byte.
  1. int main(){
  2. int size_1= sizeof(u1); //4
  3. int size_2= sizeof(u2); //4
The pointer to the union can be created just as in structure. The pointer holds the address of the union.
  1. union u_type *un;
  2. un=&u1;
  3. cout<<un->x; // 10
Like structure, the union can also be passed to the functions by both the methods i.e. call by value and call by reference.
  1. funct(u1); //calling function by call by value method.
  2. .
  3. .
  4. void funct(union u_type un){ // receving the value of the member of u1.
  5. cout<<un.x; //10
  6. }
Now, let’s call the function using call by reference method.
  1. funct(&u1); //calling function by call by refrence method.
  2. .
  3. .
  4. void funct(union u_type un){ // receving the address of u1.
  5. un->x=20
  6. .
  7. }
There is a special type of union in C++ called the Anonymous Union”. The anonymous union can not have the type name and, no variable of such union can be created. It is only to tell the compiler that its member variables are to share the same location. The variable of an anonymous union can be referred without the normal dot(.) operator.
  1. int main(){
  2. union{ //define 'anonymous union' with out the type name.
  3. int x,
  4. char c;
  5. float f;
  6. };
  7. x=10; //refered the union variable with out dot operator
  8. cout<<c; //10
  9. cout<<f; //10
  10. c=65;
  11. cout<<x; //65
  12. cout<<f; //65
  13. }
In anonymous union no member function are allowed, it can not contain private or protected data, and the global anonymous union must be specified as ‘static’.
The points to remember for the normal union declaration.
  • The definition of a union can also contain constructor and destructor.
  • The union provides the multiple ways to view the same memory location.
  • The union can not inherit the class of any type.
  • The union can not be a base class.
  • The union can not have the virtual member function.
  • The union can not have a static variable.

Key Differences Between Structure and Union

  1. The Structure uses different memory location for different members. Hence, the distinct value can be allotted to all the members. But, the union allots the same memory location to all the members. Hence, a single value can be allotted to all the members.
  2. The structure has different memory location for all members; hence, it can contain multiple values at a time, and as we know the union shares same memory location for all the members hence, it can store a single value at a time.
  3. Generally, the total size of the structure is larger than the total size of the union because the size of a structure is the sum of the size of all the members of the structures and the size of the union is the size of the member of the largest type.
  4. Structure provides single view of each location whereas, the union provides the multiple views of a single location.
  5. You can declare an anonymous union but not an anonymous structure.

Similarities:

  • Both structure and union have the same way of declaring itself, creating variables and the same way of accessing members of the variables.
  • Both Structure and union can be passed to a function by both the methods call by value and call by reference.
  • Both Structure and union are container data type & contain an object of any data type, including other structure, union, array as their members.

Conclusion:

Both, structure and union are the container datatype which contains the member of different types. But structures are used when we need to store distinct value for all the members in a distinct memory location. The unions are used when type conversion are needed.

No comments:

Post a Comment

commnet here