Difference between structure and union
Published on 06 Mar 2020
- Memory needed to store a structure variable is the sum of the bytes required for every individual member in that structure.
- All the members of a structure can be accessed simultaneously.
- Altering the value of a member will not affect the other members of the structure.
- Several members of a structure can be initialized at once.
- Easier to work with.
- Occupies more memory than a union.
- Memory needed for a union variable is equal to the memory needed for storing the largest possible member in it.
- Can access only one member at a time.
- Altering the value of a member will affect the other members of the union.
- Only the first member of a union can be initialized.
- Should keep track of the currently active member of the union.
- Comparatively, lesser memory is needed for a union variable than a structure variable of the same type
If you have any feedback about this
article and want to improve this, please write to firstname.lastname@example.org