Difference between Structure and Union – Both are user defined data types to store data of different types as a single unit. Today, We are going to share step by step Guide to check Difference between Structure and Union.
Now the differences go as follows.
1.The keyword struct is used to define a structure while 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 a structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes. While in a case of Union when a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, a size of the union is equal to the size of largest member.
3. Each member within a structure is assigned unique storage area of location while in the case of union memory allocated is shared by individual members of a union.
4. In Structure, the address of each member will be in ascending order This indicates that memory for each member will start at different offset values while in unions the address is same for all the members of a union. This indicates that every member begins at the same offset value.
5 In Structure, altering the value of a member will not affect other members of the structure while in union altering the value of any of the member will alter other member values.
6. In structure, the individual member can be accessed at a time while in unions only one member can be accessed at a time.
7.Several members of a structure can initialize at once while in unions only the first member can be initialized.
|Keyword ||Keyword |
|Example structure declaration:|
|Example union declaration:|
|Within a structure all members gets memory allocated and members have addresses that increase as the declarators are read left-to-right. That is, the members of a structure all begin at different offsets from the base of the structure. The offset of a particular member corresponds to the order of its declaration; the first member is at offset 0. The total size of a structure is the sum of the size of all the members or more because of appropriate alignment.||For a union compiler allocates the memory for the largest of all members and in a union all members have offset zero from the base, the container is big enough to hold the WIDEST member, and the alignment is appropriate for all of the types in the union.|
When the storage space allocated to the union contains a smaller member, the extra space between the end of the smaller member and the end of the allocated memory remains unaltered.
|Within a structure all members gets memory allocated; therefore any member can be retrieved at any time.||While retrieving data from a union the type that is being retrieved must be the type most recently stored. It is the programmer’s responsibility to keep track of which type is currently stored in a union; the results are implementation-dependent if something is stored as one type and extracted as another.|
|One or more members of a structure can be initialized at once.||A union may only be initialized with a value of the type of its first member; thus union |
Hope you have enjoyed reading the Difference between Structure and Union in C. If you have any query comment on below box. We will solve your query related to Difference between Structure and Union.