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.

How Do I Declare a Function Pointer in C?

291 pointsby jerryrover 8 years ago

14 comments

pettersover 8 years ago
Just use the typedef. Even if you personally find the other variants readable, chances are that your peer reading your code doesn't.
评论 #13439232 未加载
评论 #13442617 未加载
评论 #13443295 未加载
评论 #13439179 未加载
TheAdamistover 8 years ago
The new c++ alt function syntax talked about here: <a href="https:&#x2F;&#x2F;blog.petrzemek.net&#x2F;2017&#x2F;01&#x2F;17&#x2F;pros-and-cons-of-alternative-function-syntax-in-cpp&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.petrzemek.net&#x2F;2017&#x2F;01&#x2F;17&#x2F;pros-and-cons-of-alter...</a><p>mentions replacing function declarations for<p><pre><code> void (*get_func_on(int i))(int); </code></pre> with<p><pre><code> auto get_func_on(int i) -&gt; void (*)(int); </code></pre> which looks a lot more readable to me.
评论 #13440394 未加载
dnquarkover 8 years ago
The trick to reading crazy C declarations is learning the &quot;spiral rule&quot;: <a href="http:&#x2F;&#x2F;c-faq.com&#x2F;decl&#x2F;spiral.anderson.html" rel="nofollow">http:&#x2F;&#x2F;c-faq.com&#x2F;decl&#x2F;spiral.anderson.html</a> (here are more examples, with nicer formatting: <a href="http:&#x2F;&#x2F;www.unixwiz.net&#x2F;techtips&#x2F;reading-cdecl.html" rel="nofollow">http:&#x2F;&#x2F;www.unixwiz.net&#x2F;techtips&#x2F;reading-cdecl.html</a>)
评论 #13439041 未加载
评论 #13439094 未加载
评论 #13439103 未加载
cestithover 8 years ago
For anyone unable or unwilling to access that domain name for work purposes or filtering purposes, the linked page lists this alternative: <a href="http:&#x2F;&#x2F;goshdarnfunctionpointers.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;goshdarnfunctionpointers.com&#x2F;</a>
评论 #13438470 未加载
userbinatorover 8 years ago
The easiest and best way to learn the syntax is to not memorise specific cases but the grammar itself, which IMHO is no more difficult than the existing concept of operator precedence. Everyone using C should hopefully already know that multiplication has higher precedence than addition, so likewise function call (and array subscripting) has higher precedence than pointer dereference. Thus this table should make it clear that combining the two operators creates pointer-to-function:<p><pre><code> T x; T *y; T f(); T (*g)(); T pointer to T function returning T pointer to function returning T </code></pre> and the alternative, T <i>h(); , is parsed as T </i>(h()); and thus becomes &quot;function returning pointer to T&quot;.<p>The apparent struggle I see with this syntax has always somewhat puzzled me, because I don&#x27;t see the same level of complaints about e.g. arithmetic expressions (like 6+3*4&#x2F;(2+1)) which are parsed with precedence in much the same way. K&amp;R even has a section on writing a parser that recognises this syntax, so I suspect it&#x27;s really not that hard, but the perception spread by those who didn&#x27;t learn the syntax but only memorised the &quot;easy cases&quot; is making it appear more difficult than it really is.
评论 #13443970 未加载
评论 #13448031 未加载
评论 #13443593 未加载
评论 #13443415 未加载
int_19hover 8 years ago
Every time I have to deal with the declarator syntax in C or C++, I can&#x27;t help but ponder what K&amp;R were thinking when they designed this. It&#x27;s not like there weren&#x27;t other languages back then with a saner approach.<p>It looks like what they did was take the syntax from B:<p><pre><code> auto x[10]; </code></pre> and generalize it such that the type name ended up before the variable name, as in Algol. But in B this worked much better, because it didn&#x27;t have array <i>types</i> (or pointer types, or function types) - everything was a machine word. So [] in a variable declaration was just to allocate memory to which the variable would refer; the variable itself would still be a word. When they made [] part of the type, and added pointers and function types, the result was a mess.
评论 #13442131 未加载
评论 #13441649 未加载
评论 #13442766 未加载
favoritedover 8 years ago
See related:<p><a href="http:&#x2F;&#x2F;fuckingblocksyntax.com" rel="nofollow">http:&#x2F;&#x2F;fuckingblocksyntax.com</a><p><a href="http:&#x2F;&#x2F;fuckingclangwarnings.com" rel="nofollow">http:&#x2F;&#x2F;fuckingclangwarnings.com</a>
评论 #13439364 未加载
theophrastusover 8 years ago
Or if one doesn&#x27;t have cdecl installed there&#x27;s an online version[1] which has proven as a useful check on several occasions<p>[1] <a href="http:&#x2F;&#x2F;cdecl.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;cdecl.org&#x2F;</a>
评论 #13439621 未加载
bstamourover 8 years ago
This is one of those cases where I prefer C++<p><pre><code> template &lt;typename Func&gt; using function_ptr = add_pointer_t&lt;Func&gt;; </code></pre> and now declarations are a bit more sane:<p><pre><code> void foo(function_ptr&lt;void (int)&gt; callback);</code></pre>
评论 #13438743 未加载
评论 #13438847 未加载
评论 #13439794 未加载
hzhou321over 8 years ago
I never got used to having variables sandwiched inside a type. I know I am not supposed to suggest out-of-the-box, but why can&#x27;t we add a new syntax, e.g.:<p><pre><code> return_type Fn(parameters) var; typedef return_type Fn(parameters) TypeName; </code></pre> where Fn is a new keyword -- or not, if compiler understands dummy syntax -- (I would suggest &amp;lambda; when using greek letters in code become norm).<p>It simplifies the C syntax a lot IMHO.<p>PS: now I am out-of-the-box, maybe this is better:<p><pre><code> Fn{return_type, param1, param2} *var;</code></pre>
评论 #13439505 未加载
评论 #13443924 未加载
评论 #13439884 未加载
kruhftover 8 years ago
One of the only reasons I had &quot;The C Programming Language&quot; on my desk when I was a C coder. The only thing I could never remember...
shmerlover 8 years ago
The syntax is atrocious, but there isn&#x27;t much C can do about it.
cmrdporcupineover 8 years ago
Needs more profanity. The whole syntax is profane.
porjoover 8 years ago
noscript shows a nasty looking XSS warning when I click any of the &#x27;example code&#x27; links.