Live data from Hacker News

How Do I Declare a Function Pointer in C?

fuckingfunctionpointers.com

21–30 of 111 posts

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

#21
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.

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

#22

Earlier quoted context omitted.

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

Just because Linus Torvalds is a successful programmer doesn't mean he is at all correct about issues of programming. The vast majority of any of the opinions I've seen him express on programming fly in the face of good engineering practice under some guise of "real, macho programmers don't need tools". I'm not afraid to admit: I need tools. I need lots and lots of tools to do my job. The more the computer can be use…

> Just because Linus Torvalds is a successful programmer doesn't mean he is at all correct about issues of programming. The vast majority of any of the opinions I've seen him express on programming fly in the face of good engineering practice under some guise of "real, macho programmers don't need tools".

Is he wrong in this instance or our you going to ignore his point because he's not always right?

> I'm not afraid to admit: I need tools. I need lots and lots of tools to do my job. The more the computer can be used to make my job easier, the better. There is no virtue in hard work for hard work's sake.

So, as a C programmer I use lot's and lot's of tools. But that doesn't change the point that many view typedefs as bad practice[1].

[1]: http://stackoverflow.com/questions/3781932/is-typedefing-a-p...

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

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

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.

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

#25
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".

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?

#26
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.

PS: now I am out-of-the-box, maybe this is better:

    Fn{return_type, param1, param2} *var;

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

#27

Earlier quoted context omitted.

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

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 behavior, etc.

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

#28
post #8

This is one of those cases where I prefer C++ template using function_ptr = add_pointer_t ; and now declarations are a bit more sane: void foo(function_ptr callback);

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);

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

#29

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…

You'd have to call it something like `_Fn` instead, or you're going to conflict with existing code (the C standard reserves identifiers starting with an underscore followed by a capital letter; it also reserves identifiers starting with two underscores, but it's fairly common for code to ignore that and use identifiers like that anyway).

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

#30

Or if one doesn't have cdecl installed there's an online version[1] which has proven as a useful check on several occasions [1] http://cdecl.org/

cdecl is nice and all, but it will fail if there is any type that isn't a simple built-in C type. It's rare that I can just copy-paste a declaration into cdecl.
Post reply on HN