<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 10, 2021 at 4:15 AM Hal Murray via devel <<a href="mailto:devel@ntpsec.org">devel@ntpsec.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
How do I compute at compile time the offset of a field into a struct?<br>
<br>
The context is putting it into a table.<br>
</blockquote><div><br></div><div>]offsetof(struct, field)[ wrapped in macros. I pulled that from the include/json.h file of gpsd<span style="font-family:monospace"><br></span></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I know about structs and unions.  Unions are type-unsafe.  Is there a <br>
type-safe way to handle a struct that needs several variations?<br>
<br>
The struct will have a type field to tell me which variation it is.  I will <br>
have a macro to build each variation.    There is only one place that looks at <br>
the variations.  It will do a select on the type field with a few lines of <br>
code for each variation.  So all in all, it's pretty clean.  I'm just looking <br>
for a way to do better.<br></blockquote><div><br></div><div>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.</div><div><br></div><div>[1] <a href="https://stackoverflow.com/questions/42844423/writing-a-safe-tagged-union-in-c">https://stackoverflow.com/questions/42844423/writing-a-safe-tagged-union-in-c</a></div></div></div>