On Wed, Mar 10, 2021 at 4:15 AM Hal Murray via devel <devel@ntpsec.org>
wrote:

>
> How do I compute at compile time the offset of a field into a struct?
>
> The context is putting it into a table.
>

]offsetof(struct, field)[ wrapped in macros. I pulled that from the
include/json.h file of gpsd


> I know about structs and unions.  Unions are type-unsafe.  Is there a
> type-safe way to handle a struct that needs several variations?
>
> The struct will have a type field to tell me which variation it is.  I
> will
> have a macro to build each variation.    There is only one place that
> looks at
> the variations.  It will do a select on the type field with a few lines of
> code for each variation.  So all in all, it's pretty clean.  I'm just
> looking
> for a way to do better.
>

I typed in ]c tagged union[ after getting curious a while back, my bulimic
parrot level instant expert credentials for it are long expired. I remember
though that I just hit the first SO post[1] on the topic.

[1]
https://stackoverflow.com/questions/42844423/writing-a-safe-tagged-union-in-c
_______________________________________________
devel mailing list
devel@ntpsec.org
http://lists.ntpsec.org/mailman/listinfo/devel

Reply via email to