Just use the typedef. Even if you personally find the other variants readable, chances are that your peer reading your code doesn't.
No don't use typedefs there's no real reason[1] and it may cause problems. Also if your peer can't read a function pointer I don't know what help you plan on getting from them anyway, chances are you are helping/teaching them, not the other way around. [1]: http://yarchive.net/comp/linux/typedefs.html
How Do I Declare a Function Pointer in C?
41–50 of 111 posts
Re: How Do I Declare a Function Pointer in C?
#42Earlier quoted context omitted.
Thanks! Unfortunately, the page currently uses Hover's "stealth redirect" which embeds the profane URL in an iframe. So, if the profane URL is actually blocked by content filtering, you probably still won't be able to access it. I'm actively working on the page. Once it stabilizes, I'll consider mirroring a sanitized version instead of using the "stealth redirect".
Unable to access this site due to the profanity in the URL? http://goshdarnfunctionpointers.com is a more work-friendly mirror. which states: Unable to access this site due to the profanity in the URL? http://goshdarnfunctionpointers.com is a more work-friendly mirror. which is a possibly an offending link to itself. But, there is also possibly the more benignly named: http://functionpointers.com/
Re: How Do I Declare a Function Pointer in C?
#43The new c++ alt function syntax talked about here: https://blog.petrzemek.net/2017/01/17/pros-and-cons-of-alter... mentions replacing function declarations for void (*get_func_on(int i))(int); with auto get_func_on(int i) -> void (*)(int); which looks a lot more readable to me.
auto get_func_on() -> std::functionRe: How Do I Declare a Function Pointer in C?
#44For anyone unable or unwilling to access that domain name for work purposes or filtering purposes, the linked page lists this alternative: http://goshdarnfunctionpointers.com/
Thanks! Unfortunately, the page currently uses Hover's "stealth redirect" which embeds the profane URL in an iframe. So, if the profane URL is actually blocked by content filtering, you probably still won't be able to access it. I'm actively working on the page. Once it stabilizes, I'll consider mirroring a sanitized version instead of using the "stealth redirect".
Re: How Do I Declare a Function Pointer in C?
#45The trick to reading crazy C declarations is learning the "spiral rule": http://c-faq.com/decl/spiral.anderson.html (here are more examples, with nicer formatting: http://www.unixwiz.net/techtips/reading-cdecl.html )
The "spiral rule" doesn't work all the time. See this previous HN comment by stephencanon: https://news.ycombinator.com/item?id=12775862 . Also this comment by Linus Torvalds (copy pasted because I don't know how to link to Google+ comments): > I don't think that works. It breaks trivially for consecutive [] or * cases, something that he carefully didn't have in his examples. > So the examples were made up to make it…
typedef int *(**fn_t[][2])(void);
is ok, but typedef int *(**fn_t[2][])(void);
is not.(fixed formatting?)
Re: How Do I Declare a Function Pointer in C?
#46The trick to reading crazy C declarations is learning the "spiral rule": http://c-faq.com/decl/spiral.anderson.html (here are more examples, with nicer formatting: http://www.unixwiz.net/techtips/reading-cdecl.html )
No, the trick is to remember that declaration follows use . Declare a symbol using (nearly) the same exact syntax you would use to extract a value of the base type from that symbol. See also my comment last time this subject came up: https://news.ycombinator.com/item?id=12775966
int* p; // p is an int pointer
instead of int *p; // dereferencing p will give an int
I know this is the subject of holy wars, but once I'd seen the second one my eyes were opened and I had way less trouble. I think that declaration follows use is another of example of the amazing design powers of the patriarchs.Re: How Do I Declare a Function Pointer in C?
#47For anyone unable or unwilling to access that domain name for work purposes or filtering purposes, the linked page lists this alternative: http://goshdarnfunctionpointers.com/
Thanks! Unfortunately, the page currently uses Hover's "stealth redirect" which embeds the profane URL in an iframe. So, if the profane URL is actually blocked by content filtering, you probably still won't be able to access it. I'm actively working on the page. Once it stabilizes, I'll consider mirroring a sanitized version instead of using the "stealth redirect".
Re: How Do I Declare a Function Pointer in C?
#48Earlier quoted context omitted.
No, the trick is to remember that declaration follows use . Declare a symbol using (nearly) the same exact syntax you would use to extract a value of the base type from that symbol. See also my comment last time this subject came up: https://news.ycombinator.com/item?id=12775966
And yet so many people learn int* p; // p is an int pointer instead of int *p; // dereferencing p will give an int I know this is the subject of holy wars, but once I'd seen the second one my eyes were opened and I had way less trouble. I think that declaration follows use is another of example of the amazing design powers of the patriarchs.
Thanks for elevating their gender specifically.... gosh forbid that Ada had any amazing design powers.
Re: How Do I Declare a Function Pointer in C?
#49Just use the typedef. Even if you personally find the other variants readable, chances are that your peer reading your code doesn't.
Personally I don't like when people hide a pointer behind a typedef. If you want to use a typedef, typedef the function and then declare a pointer to that: typedef int func(void); func *func_ptr; Avoids the mess of the function pointer syntax, but still makes the fact that it is a pointer clear.
Do you remember where you picked this up? Any particular book or codebase?
Re: How Do I Declare a Function Pointer in C?
#50Earlier quoted context omitted.
And yet so many people learn int* p; // p is an int pointer instead of int *p; // dereferencing p will give an int I know this is the subject of holy wars, but once I'd seen the second one my eyes were opened and I had way less trouble. I think that declaration follows use is another of example of the amazing design powers of the patriarchs.
> amazing design powers of the patriarchs Thanks for elevating their gender specifically.... gosh forbid that Ada had any amazing design powers.