TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Learning that you can use unions in C for grouping things into namespaces

167 pointsby deafcalculusalmost 4 years ago

11 comments

10000truthsalmost 4 years ago
Anonymous nested structs are also quite useful for creating struct fields with explicit offsets:<p><pre><code> #include &lt;stdio.h&gt; #include &lt;stdint.h&gt; #define YDUMMY(suffix, size) char dummy##suffix[size] #define XDUMMY(suffix, size) YDUMMY(suffix, size) #define PAD(size) XDUMMY(__COUNTER__, size) struct ExplicitLayoutStruct { union { struct __attribute__((packed)) { PAD(3); uint32_t foo; }; struct __attribute__((packed)) { PAD(5); uint16_t bar; }; struct __attribute__((packed)) { PAD(13); uint64_t baz; }; }; }; int main(void) { &#x2F;&#x2F; offset foo = 3 &#x2F;&#x2F; offset bar = 5 &#x2F;&#x2F; offset baz = 13 printf(&quot;offset foo = %d\n&quot;, offsetof(struct ExplicitLayoutStruct, foo)); printf(&quot;offset bar = %d\n&quot;, offsetof(struct ExplicitLayoutStruct, bar)); printf(&quot;offset baz = %d\n&quot;, offsetof(struct ExplicitLayoutStruct, baz)); return 0; }</code></pre>
评论 #28029184 未加载
评论 #28028032 未加载
评论 #28032024 未加载
评论 #28028231 未加载
评论 #28029168 未加载
flohofwoealmost 4 years ago
I&#x27;m using anonymous nested structs extensively for grouping related items, but I consider the extra field name a feature, not something that should be hidden:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;floooh&#x2F;sokol-samples&#x2F;blob&#x2F;bfb30ea00b5948f2029c7dd228cf30de08b8036c&#x2F;sapp&#x2F;shdfeatures-sapp.c#L136-L165" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;floooh&#x2F;sokol-samples&#x2F;blob&#x2F;bfb30ea00b5948f...</a><p>(also note the &#x27;inplace initialization&#x27; which follows the state struct definition using C99&#x27;s designated initialization)
kevin_thibedeaualmost 4 years ago
The result is uglier and less maintainable than a pair of macros. Or just stop trying to hide syntax. This is ultimately on the same level as typedefing pointers.
remramalmost 4 years ago
The first example seems wrong, instead of `struct sub { ... };` what is meant is `struct { ... } sub;`
评论 #28030713 未加载
sesuximoalmost 4 years ago
Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” member).
评论 #28027362 未加载
评论 #28027476 未加载
评论 #28027206 未加载
评论 #28029207 未加载
bruce343434almost 4 years ago
Imo this is not “perverse”. In my vector library I alias a vec3 as float x,y,z and float[3] using this technique.
评论 #28029196 未加载
PaulHoulealmost 4 years ago
The C programming language, brought to you by Cthulhu.<p>You don&#x27;t need eval(), you&#x27;ve got strcpy()!
rightbytealmost 4 years ago
I don&#x27;t regard this as a &quot;perverse&quot; hack. If I ever do embedded memory mapped stuff in C11 this is way too tempting.
评论 #28029221 未加载
Subsentientalmost 4 years ago
Bleurgh. I have a deep soft spot for C, and I&#x27;m known to get twisted pleasure from using obscure language features in new ways to annoy people, but this is a level of abuse that even I can&#x27;t get behind. If you need namespacing, use C++. As much as I love C, it&#x27;s terrible for large projects.
评论 #28028854 未加载
评论 #28030070 未加载
评论 #28028398 未加载
midjjialmost 4 years ago
This is probably a terrible idea, remember that if you have written one member of a union, all other members remain public, yet accessing any of them in any way is undefined behaviour. This is made way worse by most compilers mostly choosing to let you do what you think it will. They just dont guarantee they always will or in all cases.
评论 #28029590 未加载
adamnemecekalmost 4 years ago
Don&#x27;t actually do this.
评论 #28027729 未加载