Difference between Structure and UnionStructure and union both are user-defined data types in the C/C++ programming language. In this section, we will see what the Structure and Union are; and the differences between them. What is a structure (struct)?Structure (struct) is a user-defined data type in a programming language that stores different data types' values together. The struct keyword is used to define a structure data type in a program. The struct data type stores one or more than one data element of different kinds in a variable. Suppose that you want to store the data of employee in your C/C++ project, where you have to store the following different parameters:
One way to store 4 different data by creating 4 different arrays for each parameter, such as id[], name[], department[], and email[]. Using array id[i] represents the id of the ith employee. Similarly, name[i] represents the name of ith employee (name). Array element department[i] and email[i] represent the ith employee's department and email address. The advantage of using a separate array for each parameter is simple if there are only a few parameters. The disadvantage of such logic is that it is quite difficult to manage employee data. Think about a scenario in which you have to deal with 100 or even more parameters associated with one employee. It isn't easy to manage 100 or even more arrays. In such a condition, a struct comes in. Syntax of structExampleWhat is a Union?In "c," programming union is a user-defined data type that is used to store the different data type's values. However, in the union, one member will occupy the memory at once. In other words, we can say that the size of the union is equal to the size of its largest data member size. Union offers an effective way to use the same memory location several times by each data member. The union keyword is used to define and create a union data type. Syntax of UnionExampleExample of Structure and Union showing the difference in CLet's see an example of structure (struct) and union in c programming, illustrating the various differences between them. Output data of structure: integer: 5 decimal: 15.00 name: John data of union: integer: 5 decimal: 0.00 name: Accessing all members at a time: data of structure: integer: 163 decimal: 75.000000 name: John data of union: integer: 1852337994 decimal: 17983765624912253034071851008.000000 name: John Accessing one member at a time: data of structure: integer: 140 decimal: 150.000000 name: Mike data of union: integer: 1701538125 decimal: 69481161252302940536832.000000 name: Mike Altering a member value: data of structure: integer: 512 decimal: 150.00 name: Mike data of union: integer: 512 decimal: 0.00 name: sizeof structure: 28 sizeof union: 20 Difference between Structure and UnionLet's summarize the above discussed topic about the Struct and Union in the form of a table that highlight the differences between structure and union:
Comparative advantages and disadvantages of the structureBelow is the list of some advantages and disadvantages of using structure: Advantages of Structure
Disadvantages of Structure
Comparative advantages and disadvantages of unionBelow is the list of some advantages and disadvantages of using union: Advantages of Union
Disadvantages of Union
Next TopicDifference between |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India