The way i understood it is that a union is a sneaky way of using the same name for several types of variables while only one type can be used at any given time with that name.
So if we had a union "MyVar" that was declared as both float and byte we could store EITHER a byte or a float there, something we cant normally do with either a regular float variable or regular byte variable. Note that we can NOT store BOTH there at the same time, we can only store one at a time. If we store a byte there and then store a float there, the float overwrites the byte and the byte is no longer available for retrieval, and if we store a float there and then store a byte, the byte overwrites the float and the float is no longer available for retrieval.
So whatever type we store there last is the only type available for reading.
For example if we store 0x12 we have only 0x12 available, and if we later store 1.234 there then we loose the 0x12 and only retain the 1.234. If we then later store 0x34 there then we lose the 1.234 and only have the 0x34 available.
We can make our own custom unions through the use of allocated memory, but i think the union is more compiler friendly.
I cant remember the errors associated with unions offhand though, but with allocated memory there will be none as long as you dont write outside of the memory space allocated. This means that if you allocated 4 bytes you can store a 32 bit integer there or 4 distinct bytes. There is a chance this might be the same with a union though if we declare one integer or 4 bytes but i dont remember if that is possible or not.
So if we had a union "MyVar" that was declared as both float and byte we could store EITHER a byte or a float there, something we cant normally do with either a regular float variable or regular byte variable. Note that we can NOT store BOTH there at the same time, we can only store one at a time. If we store a byte there and then store a float there, the float overwrites the byte and the byte is no longer available for retrieval, and if we store a float there and then store a byte, the byte overwrites the float and the float is no longer available for retrieval.
So whatever type we store there last is the only type available for reading.
For example if we store 0x12 we have only 0x12 available, and if we later store 1.234 there then we loose the 0x12 and only retain the 1.234. If we then later store 0x34 there then we lose the 1.234 and only have the 0x34 available.
We can make our own custom unions through the use of allocated memory, but i think the union is more compiler friendly.
I cant remember the errors associated with unions offhand though, but with allocated memory there will be none as long as you dont write outside of the memory space allocated. This means that if you allocated 4 bytes you can store a 32 bit integer there or 4 distinct bytes. There is a chance this might be the same with a union though if we declare one integer or 4 bytes but i dont remember if that is possible or not.