struct and union in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

struct and union in C

I understand the main difference between structs and union but I don't understand the advantage of union.

1st Nov 2020, 10:16 PM
Meow😾
Meow😾 - avatar
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.
1st Nov 2020, 11:27 PM
Davide
Davide - avatar