struct and union in C | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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 ответ
+ 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