+ 2
struct and union in C
I understand the main difference between structs and union but I don't understand the advantage of union.
1 Answer
+ 3
Suppose you want to store position and momentum of subatomic particles. You know that for each particle you can't have both at the same time. So to save memory you store your data in a uninon.
For some particle you'll store the position, for some other the momentum. But you are never spending memory on both of them for a given particle, unlike you would do using a structure.