Structure vs Union in C
Bothstruct and union are user-defined data types in C that group multiple members together. They differ fundamentally in how memory is allocated and how members are accessed.
Comparison Table
| Feature | Structure (struct) | Union (union) |
|---|---|---|
| Keyword | struct | union |
| Memory | Own memory per member | Shared memory |
| Size | Sum of all members | Size of largest member |
| Values held | All at once | Only the last-written one |
| Access | Multiple at once | One at a time |
| Initialization | Multiple members | Only the first member |
| Use case | Group related fields of different types | Store one of several types at a time |