Structure | Union |
---|---|
struct keyword is used to define a structure | union keyword is used to define a union |
In structure, each member get separate space in memory | Total memory space allocated is equal to the member with the largest size |
All other members use its own memory space | All other members share the same memory space |
All the members can be initialized while declaring the variable | The only first member can be initialized while declaring the variable |
An individual member can be accessed at a time | Only one member can be accessed at a time |
Syntax: struct [structure name] { member definition; member definition; … }; | Syntax: union [union name] { member definition; member definition; … }; |
Example: struct demo { int integer; float x; } | Example: union demo{ int integer;{ float x;{ } |
It is very easy to learn and understand the structure and union from this difference very niceπ
Thanks. π Like and Share