+ 2
union can only store one value at a time, and that means that the size of the union is the size of its greatest element. example: union { uint8_t aByte; uint16_t twoBytes; } myUnion; myUnion variable will have the size of two bytes and is going to store either an uint8_t or a uint16_t, but never both at the same time. A struct is NOT going to store one value at a time and is going to have the size of the sum of its elements' size. struct myStruct_t { uint8_t oneByte; uint16_t twoBytes; }; this struct takes 3 bytes of memory space and can store both an uint8_t and a uint16_t at the same time. Also note that an struct can have functions, constructors... every thing a class can have.
25th Jul 2016, 9:05 AM
ddrodoric