r/C_Programming • u/No_Insurance_6436 • Sep 07 '26
Question Should I use nested structs, separate structs, or union nested in a struct for this?
I am doing my first game-type project in C. I am having trouble making a decision regarding handling UI. I am awful at explaining things and new to this so please bear with me.
I plan to have a struct UIElement that contains information related to a particular element. However, there will be different types of elements, such as Text, Texture, Color and whatnot. They will also contain a pointer to an array other UIelements belonging to them, for things such as a buttons on a window.
I am stuck between the following implementations for this:
Make a struct "UIElement" that has information that every element will have (such as position)., then have other structs defined such as "TextElement" that will have a pointer to a UIElement as well as Text-specific information.- Use enum and nested union within the UIElement struct to allow different instances of the same struct to have only the relevant data necessary for them. (Each element has enum for it's type within it)
- Just have different structs "TextElement", "ColorElement", "TextureElement". Doing this I believe I would have to use void* to store pointers to elements and cast them accordingly.
After writing this, option 2 seems like the best option, but the simplicity of 3 sounds useful, albeit bad in the long run. If I am overlooking a simple way to do this, please let me know.
4
u/Ill_Specialist8564 Sep 07 '26
go with option 2. put the enum and a union inside UIElement.
i build ui systems this way and it removes all allocation headaches. you store your elements in one array or pass them around as UIElement pointers without caring about the type until you hit the switch on that enum. option 1 forces you to cast pointers back and forth every time you read text data from a TextElement. option 3 puts you right back into void pointer casting which destroys whatever help the compiler gives you. allocate one struct, set the tag, write to the union member, move on
2
u/glasket_ Sep 07 '26 edited Sep 07 '26
The second form is called a tagged or discriminated union and is an extremely common way to handle this problem.
Edit: As an addition, 1 is composition. It largely depends on if you want to operate on
UIElementas a type which can be one of multiple types, or if you want to operate on each type as an independent type while having a shared basis for certain functions. This is an API design decision.5
u/FancySpaceGoat Sep 07 '26
You can make UIElement a prefix struct. It contains the enum and any other universally shared properties.
TextElement and others would then each start with a UIElement as their first member.
You can then cast from a UIElement* to a pointer of the corresponding struct when the enum matches.
It's effectively the same as the nested union, but with the added benefit of not padding everything to the largest size
3
u/detroitmatt Sep 07 '26
and so on. You don't have to store void*. You can store struct UIElement*. A ColorElement* is convertible to a UIElement* if its first member is struct UIElement. And if you happen to know that your UIElement* does in fact point to a struct ColorElement, then you can do vice versa too.
That means you will want to have some way to keep track of what the UIElement* you're passing around is. It may be wise to have:
Might not be necessary depending on your code, but I'd say to just add them.
1
u/flatfinger Sep 11 '26
Nearly all compilers can be configured to process a dialect of C in which an expression like
foo->barwill take the address foo, displace it byoffsetof(typeof(*foo), bar)bytes, and access atypeof(foo->bar)at that address, in a manner that is agnostic with regard to how the storage at that location might previously have been accessed. Although the Standard waives jurisdiction over many corner cases that would be associated with such constructs, in order to avoid requiring that compilers correctly handle tricky corner cases that might not be relevant to all applications, nearly all compilers can be configured to process those corner cases as described without regard for whether the Standard would require that they do so. In clang and gcc, this is enabled via the-fno-strict-aliasingoption.When using such dialects, a common pattern is to have possibly overlapping groups of structure types lead off with an initial sequence of things which are representation-compatible with the corresponding members of other structures in their group. One might, for example, have shape objects lead off with a pointer to a structure holding pointers to functions that would act on things of that type, followed by a length and a flags word, followed (in relevant types) by base X and Y coordinates and (in relevant types) width and height. The flags byte might be used to distinguish graphical marker objects that don't have a position, those that have a position but not an area, or those that have a defined area.
Note that clang and gcc are not designed to process such constructs correctly in cases where the Standard does not require that they do so unless
-fno-strict-aliasingflag is used, and the range of cases they are designed not to handle correctly contains enough surprises that I'd suggest using that option in any case.0
u/iamfacts Sep 07 '26
You're looking at the problem from a very oop pov. The thing is, at the end of the day, you just need the cpu to draw some text and /or bg, etc. somewhere on the screen.
So, I would do something like this -
And that's it. I wouldn't bother abstracting every widget kind or subtypes. Simply no need. Oh, and have a bunch of flags to disambiguate behaviour and style.