What is the difference between struct and union?



Suraj Said

+0 -0

structs are used to group multiple variables of different types together under a single name, each variable has its own memory space.
Unions on the other hand, share a memory space among all of its members, so only one member can be accessed at a time.
Structs are typically used to represent complex data structures, while unions are used when memory space is a concern and only one of the values of different types is needed at a time.



Priya Said

+0 -0

A struct stores multiple values of different types simultaneously, while a union stores one value at a time, sharing memory among its members.



Aditya Said

+0 -0

Struct members can have different data types, each member has its own memory space. Union members share the same memory space, only one member can be active at a time. Struct is used to represent a collection of related data items, while union is used to represent a single data item that can take different forms.



Submit Your Response