Live data from Hacker News

How Do I Declare a Function Pointer in C?

fuckingfunctionpointers.com

31–40 of 111 posts

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

#31
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

thing is, in practice it gets used as a type. If you repeat that function definition more than once you are creating a bit of a nightmare to maintain.

For instance if you need to add an extra parameter, you can easily find all usages. If something else uses the same definition, but is used for a different purpose, it gets much harder to distinguish the different usages. especially for a good ol void (callback)(void context) which a number of event's may use, and then later some may callback with more information which can be changed in once place and the compiler will quickly show you what breaks.

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

#32

Earlier quoted context omitted.

That post does not say "don't use typedefs there's no real reason." It says "don't use typedefs without a real reason." Linus even provides a few examples of good uses of typedefs. Specifically he's talking about typedef'ing structs to a named type to hide that it's a struct. That's different from typedef'ing a function pointer so that you don't need to be a C parsing expert to read the code.

> That's different from typedef'ing a function pointer so that you don't need to be a C parsing expert to read the code. If you can't read C code you should work on your C programming ability, there is nothing difficult about reading function pointers. Edit: If people are getting tripped up with basic syntax good luck with the actual hard parts of C, Like lack of memory safety, concurrent memory management, undefined…

It's not just about readability, it's about making a contract with the user. If you typedef a complex struct in your library you're telling your users to treat this type like it's a black-box and not look inside because the next version of the library might contain something different and your code will break if you depend on it.

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

#33
post #32

Earlier quoted context omitted.

> That's different from typedef'ing a function pointer so that you don't need to be a C parsing expert to read the code. If you can't read C code you should work on your C programming ability, there is nothing difficult about reading function pointers. Edit: If people are getting tripped up with basic syntax good luck with the actual hard parts of C, Like lack of memory safety, concurrent memory management, undefined…

It's not just about readability, it's about making a contract with the user. If you typedef a complex struct in your library you're telling your users to treat this type like it's a black-box and not look inside because the next version of the library might contain something different and your code will break if you depend on it.

> It's not just about readability, it's about making a contract with the user. If you typedef a complex struct in your library you're telling your users to treat this type like it's a black-box and not look inside because the next version of the library might contain something different and your code will break if you depend on it.

No need to use typedef, just use an opaque struct if you want something to behave like a blackbox.

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

#35

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't we add a new syntax, e.g.: return_type Fn(parameters) var; typedef return_type Fn(parameters) TypeName; where Fn is a new keyword -- or not, if compiler understands dummy syntax -- (I would suggest λ when using greek letters in code become norm). It simplifies the C syntax a lot IMHO…

IIRC the former is basically what D does. The keyword is `function`.

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

#36

Earlier quoted context omitted.

In C, the easiest thing to do is to just use a typedef. You get to use the same syntax as for normal function calls with no trying to remember where that blasted extra * goes, and typically end up with cleaner code anyway. typedef void function_ptr_t(int i); function_ptr_t* ptr;

Yeah, typedefs solve the problem for fixed types. I guess you could use a macro to create a generic FUNC_PTR or something as well.

Macros are often a bad idea for defining types. If you do

    #define INT_PTR int*
    INT_PTR p, q;
the code is actively misleading. Is there a better way to do it for function pointers?

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

#37
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…

All these years I assumed that the 'spiral rule' and 'right-left rule' (linked from the stephencanon comment above, and also described in my second link) are two ways to describe the same algorithm, but, reading closely, they aren't! I guess 'spiral rule' is a stickier name, which is why it's something that people remember even though it's janky.

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

#38

Earlier quoted context omitted.

In C++, pointers to member functions are even more cumbersome than C function pointers.

auto greet = std::mem_fn(&Foo::display_greeting); Looks pretty simple to me, much simpler than C function pointers. :) Pairs nicely with std::bind, too, like so: Foo foo; std::function setter = std::bind(&Foo::setValue, &foo, std::placeholders::_1); setter(42);

If you need to keep that pointer in a collection, or in a class member, or pass as a function argument, “auto” won’t work.

And std::function introduce some performance overhead.

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

#39

Earlier quoted context omitted.

Yeah, typedefs solve the problem for fixed types. I guess you could use a macro to create a generic FUNC_PTR or something as well.

Macros are often a bad idea for defining types. If you do #define INT_PTR int* INT_PTR p, q; the code is actively misleading. Is there a better way to do it for function pointers?

There are a few other problems with multiple declarations aren't there? I though they were discouraged in c.
Post reply on HN