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.

Multiple return values in C at the cost of one weird input parameter

22 pointsby Proceduralabout 7 years ago

9 comments

beeforporkabout 7 years ago
Sorry to be blunt -- I mean no offense, we all started somewhere. To me, it reads like a beginners experiment. All that #pragma stuff, that first param (&#x27;zero&#x27;) that is all not needed for the effect. The &#x27;zero&#x27; and &#x27;return;&#x27; stuff reads to me like someone is assuming typeof() evaluates its argument so someone must do somethings against it executing. It looks bloated with stuff a beginner does not realise is unnecessary.<p>I suppose the point is to declare a tuple directly as a return type without a typedef. Then, to return a value, use &#x27;typeof&#x27; to declare a return variable of that anonymous type. So, the whole point is to avoid a typedef, I think:<p><pre><code> typedef struct { int x; int y; } foo_result_t; foo_result_t foo(int a, int b) { return (foo_result_t){ a*a, a&#x2F;b }; } int main(void) { foo_result_t a = foo(40,2); } </code></pre> Instead of typedef, use typeof and __auto_type. Without the bloat:<p><pre><code> struct { int x; int y; } foo(int a, int b) { typeof(foo(a,b)) out = { a*a, a&#x2F;b }; return out; } int main(void) { __auto_type a = foo(40,2); } </code></pre> It is a bit underwhelming. But it tought me something: I did not know GCC&#x27;s (and Clang&#x27;s?) __auto_type extension (new since gcc 4.9, apparently). It looks great for compiler specific magic macros! :-)
chewxyabout 7 years ago
Isn&#x27;t this how any multiple returns are handled? Return a tuple. Then language supports allow splatting of it, that&#x27;s all.<p>What am I missing?
评论 #16727506 未加载
userbinatorabout 7 years ago
I guessed the &quot;one weird input parameter&quot; would simply be the pointer to the structure to &quot;return&quot; multiple values, but it turns out I was wrong --- it is using the syntax of returning a structure by value, which depending upon the exact compiler, could be implemented in much the same way as an out-pointer parameter. i.e.<p><pre><code> struct Foo foo = return_foo();</code></pre> becomes<p><pre><code> struct Foo foo; return_foo(&amp;foo); </code></pre> That said, the ability to pass, return, and assign structures by value seems to be one of the lesser-known features of C, so this serves as a good example.
评论 #16727173 未加载
tehwalrusabout 7 years ago
Yeah, just return the struct by value.<p>By the way, if you do C programming and haven&#x27;t, read 21st Century C:<p><a href="http:&#x2F;&#x2F;shop.oreilly.com&#x2F;product&#x2F;0636920033677.do" rel="nofollow">http:&#x2F;&#x2F;shop.oreilly.com&#x2F;product&#x2F;0636920033677.do</a>
mar77iabout 7 years ago
How about<p><pre><code> return out; (void)zero; </code></pre> instead of the whole preprocessor magic for clang?<p>And while we&#x27;re taking suggestions, use a named struct, that keeps you from making up a new type for every case.
yamiabout 7 years ago
- Step 1 return struct<p>- Step 2 post on HN<p>- Step 3 ???<p>- Step 4 Enjoy
th3iedkidabout 7 years ago
Did not understand what is so special?
评论 #16727135 未加载
imodeabout 7 years ago
how did this make the front page?<p>it&#x27;s a demonstration of returning a struct by value...<p>how is this...<p>1. multiple return values? 2. costing one &quot;weird input parameter&quot;? 3. worthy of a front-page mention?
redipyniabout 7 years ago
Compilers hate him!