Guidelines and tips for mixing up C structure and union. | SoloLearn: Learn to code for FREE!

+11

# Guidelines and tips for mixing up C structure and union.

I understood that struct and union are used to group data as a pack, with an exception that union stores its member values all in the same address in memory. I would like to know more about these data structures, so please share anything you know about them that I haven't mentioned. Differences, strengths and/or weaknesses, usage preferences etc. My first question is about mixing the two data structures, e.g. a struct inside a union, or a union inside a struct. Tell me when/why would/should we do that (mixing them up)? How (or in which situation) do we decide which should be inside the other, and what are the points to notice, the DOs and DON'Ts. Second question is about anonym struct or union and/or mix of the two. What/when/why the need to use these? what are the benefits (if any) over the use of named struct or union? Third question is, what difference between C and C++ union. I understand the difference between C and C++ struct, so it's okay 👌 Thanks in advance, 🙏

1/26/2020 5:50:54 PM

Ipang

+8

I usually use a union for 2 things. - If I want to return 1 and only 1 type among a select few. - If I want to return 1 type and be able to manipulate it's internal bits with other types. In the first case, returning just a union would be a mistake, because there is no way to determine which type is active. For example if a function returned a union{ float, int } then how do you know if the active value is the float or the int? The solution is to nest a union inside a struct and use another identifier that you place inside the struct. For example struct{ bool identifier; union { float f; int i; }; }; The function would set the identifier to 0, if the active type is the float, or a 1 if the active value is the int. Of course I just use a bool because there are only 2 possibilities, you'd use a different type for the id if there were more. If I have to use a union, this is the one I go for.(before C++17) For example I use this in my math expression parser so that it can switch to floats if decimal points get involved while using integrals when not. - The 2nd case is actually much rarer, the only time I remember using it was when I was making my gameboy emulator. You see, the gameboy has 8 8 bit registers. However, the gameboy also has the ability to combine them together to form 4 16 bit registers. So the way you could go about it is to use a union like union Register { std::uint16_t u16; struct { std::uint8_t l; std::uint8_t h; }; }; This way you can easily access both versions without needing to use bit manipulation. ( although I did opt for just that later on though ) The problem with the struct inside the 2nd union is that it totally depends on the endianness of the machine, not to mention that anonymous structs are illegal C++ ( without an extension on the language ) but legal in C. (C11 I believe). Accessing non active types in C++ is also undefined behaviour, but not in C. In C++17 you should use std::variants instead. ( out of chars )

+7

Wow Ipang that is a mouthful where to begin outside of giving you the GeeksforGeeks reference as a starting point... https://www.geeksforgeeks.org/difference-structure-union-c/ I know that you do a lot of reading and always give good answers. Maybe we can set the stage from what GeeksforGeeks explains.

+7

In programs that communicate with each other over a network there could be different types of messages, e.g. with the first field indicating the message type. A union could be used for the message data structure in this case as the fields of the message could vary depending on the type of message. A given type of message could also have certain composite fields within it and in this case, you could use a struct (within the union) to represent the composite field.

+6

A little bit of information here https://code.sololearn.com/ce4LYug0YIfN/?ref=app union are used when you limited memory resources. Low level system and kernel programming. Can be used for non-portable data type conversion (may not always work)

+4

Thank you BroFar 🙏

+3

Thank you ~ swim ~ 🙏

+2

Thank you Dennis 🙏

+2

Thank you Coder Kitten 🙏

+2

Thank you Sonic 🙏