C++ 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
Comparison Chart
BASIS OF COMPARISON | STRUCTURE | UNION |
---|---|---|
Basic | The separate memory location is allotted to each member of the 'structure'. | All members of the 'union' share the same memory location. |
Declaration | struct struct_name{ type element1; type element2; . . } variable1, variable2, ...; | union u_name{ type element1; type element2; . . } variable1, variable2, ...; |
keyword | 'struct' | 'union' |
Size | Size of Structure= sum of size of all the data members. | Size of Union=size of the largest members. |
Store Value | Stores distinct values for all the members. | Stores same value for all the members. |
At a Time | A '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 Viewing | Provide single way to view each memory location. | Provide multiple way to to view same memory location. |
Anonymous feature | No 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.
- struct employee{
- string name;
- string company_name;
- string city;
- }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.
- 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.
- emp1.name="ashok";
The information contained in one structure variable can be assigned to another structure variable of the same type as follow.
- emp1.name="ashok";
- emp1.company_name= "Teckpix";
- emp1.city="Delhi";
- emp2=emp1; // assigning values of the member of 'emp1' to 'emp2'.
- cout<<emp2.name; //ashok
- cout<<emp2.company_name; //Teckpix
- 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.
- funct(emp1.city);
The entire structure variable can be passed to the function, by both the methods call by value and call by reference.
- funt(emp1); // calling function by 'call by value' method.
- .
- .
- void funct(struct employee emp){ // receving values of members of emp1.
- cout<<emp.city; //Delhi
- .
- }
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.
- funt(&emp1); // calling function by 'call by reference' method.
- .
- .
- void funct(struct employee *emp){ // receving address of emp1.
- emp->city="Nagpur"; // change the value of the member(city) of structure variable emp1.
- .
- }
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.
- srtuct employee *emp; //decalring the structure pointer of type 'employee'.
- emp=&emp1; // assigning the address of 'emp1' to the pointer.
- 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.
- int main(){
- struct epm3={"Anil", "Teckpix", "Nagpur"};
- } // 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.
- union u_type{
- int x,
- char c;
- float f;
- }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.
- int main(){
- union u_type u1, u2; //The keyword union is not mandatory in C++.
- }
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.
- 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.
- u1.x=10;
- cout<<u1.c; //10
- 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.
- u1.c=65;
- cout<<u1.x; //65
- 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.
- int main(){
- int size_1= sizeof(u1); //4
- 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.
- union u_type *un;
- un=&u1;
- 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.
- funct(u1); //calling function by call by value method.
- .
- .
- void funct(union u_type un){ // receving the value of the member of u1.
- cout<<un.x; //10
- }
Now, let’s call the function using call by reference method.
- funct(&u1); //calling function by call by refrence method.
- .
- .
- void funct(union u_type un){ // receving the address of u1.
- un->x=20
- .
- }
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.
- int main(){
- union{ //define 'anonymous union' with out the type name.
- int x,
- char c;
- float f;
- };
- x=10; //refered the union variable with out dot operator
- cout<<c; //10
- cout<<f; //10
- c=65;
- cout<<x; //65
- cout<<f; //65
- }
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
- 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.
- 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.
- 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.
- Structure provides single view of each location whereas, the union provides the multiple views of a single location.
- 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