Live data from Hacker News

How Do I Declare a Function Pointer in C?

fuckingfunctionpointers.com

41–50 of 111 posts

Re: How Do I Declare a Function Pointer in C?

#41
post #15

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

Most of those are against typedefs for structs, and in the last message, he says that using a typedef for a complex function pointer is "just common sense".

Re: How Do I Declare a Function Pointer in C?

#42
post #3

Earlier 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/

Even the last one is blocked for me. Luckily there is only one IT guy for the entire company and he will generally whitelist anything you ask for.

Re: How Do I Declare a Function Pointer in C?

#43

The 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.

I'd say this is even more readable:

  auto get_func_on() -> std::function

Re: How Do I Declare a Function Pointer in C?

#44
post #3
post #2

For 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".

Not to infringe on your free speech, but you could save yourself some time and do your potential readers a favor if you just fucking didn't swear in your domain name. :)

Re: How Do I Declare a Function Pointer in C?

#45
post #7

The 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?

#46
post #7

The 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

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.

Re: How Do I Declare a Function Pointer in C?

#47
post #3
post #2

For 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".

Wait, what type of fucked up world do people exist in where a website is blocked due to the word "fuck".

Re: How Do I Declare a Function Pointer in C?

#48
post #46

Earlier 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.

> 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?

#49
post #15

Just 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.

It's never even occurred to me to typedef a function like this, and now that I think about it I'm not sure why. Your way is a lot clearer. Thanks for the tip.

Do you remember where you picked this up? Any particular book or codebase?

Re: How Do I Declare a Function Pointer in C?

#50
post #48
post #46

Earlier 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.

Wasn't C designed by a man?
Post reply on HN