I am creating a "combined variable" that occupies two bytes and those two bytes can be identified as either foon to write to the full 16 bit (2 byte) item in one go:īut if you look at 12345 in hex it is 0x3039. A union is just "two types of variable on top of one another" (and before the purists tell me "no it isn't" bear with me as that is the easiest way to imagine it). Jim it's really simpler than you might imagine. Jgmdesign wrote: Unions and Structures have always been a struggle for me and both of your examples make no sense on how you get an integer from the two bytes. It's the same mechanism as stacking two 8-bit variables in the same 16-bit union, and has all the same issues regarding endian-ness and packing the latter can be particularly headache inducing if the compiler generating the messages isn't the same as the one receiving them (Windows sender to IAR ARM receiver in my case) - there's a reason unions are banned in MISRA compliant code. Without having to parse each message individually. Union_name->structure_name.field_name // when passed a pointer to the union The reception protocol sorts out getting the message and making sure it's valid - for which it needs to know nothing about the structure contents - and then individual messages parts can be accessed using I have used them to manage a messaging bitstream where a number of different messages share a common header but completely different data structures: Each message type has a structure describing it the union is a union of all the possible messages. Jgmdesign wrote: I will do more research on Structures and unions as they are probably far more eloquent in many applications.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |