Live data from Hacker News

How Do I Declare a Function Pointer in C?

fuckingfunctionpointers.com

51–60 of 111 posts

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

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

Not the OP, but given that English is historically gender biased, I find it pretty easy to unconsciously fall into the trap of using a gender biased turn of phrase. Even if one is consciously trying to avoid such things, our messy neural nets being what they are, mistakes happen.

Given the almost certainty (in my mind anyway), that the OP meant no disrespect to people who identify with genders that are not male, it would make me very happy indeed if requests for correction could be made in a respectful way. Simply asking something like "Would you mind using the alternative phrasing 'blah blah blah'" would go a long way towards helping everyone to maintain a civil tone.

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

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

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

Why did you post an empty comment?

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

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

Correct me if I'm wrong, but isn't a "function" always a pointer in C? That is, there's no such thing as a "value function" in C, right?

Given that, what's the advantage for "your version" of the idiom?

(This may just be nitpicking.)

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

#54

Earlier quoted context omitted.

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.

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

Trivially solved with decltype, no need to remember anything.

decltype(std::mem_fn(&Foo::Whatever))

Typedef it if you want.

> And std::function introduce some performance overhead.

It's the same performance as a virtual function call. Pretty much the same as a function pointer invoke.

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

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

how do you remember a const pointer like

> int * const a

? i.e. in what way do you extend your scheme such that it makes sense again?

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

#56

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…

Good luck parsing this:

  double (*my_function(int(*callback)(void*)))(double);
without needing to think way harder on it than if typedefs were used:

  func2* my_function(func1*)
This isn't basic syntax. C's parser is really bloody complex. That's why we have things like ioccc.org, because C's syntax is NOT trivial.

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

#57

Earlier quoted context omitted.

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.

Correct me if I'm wrong, but isn't a "function" always a pointer in C? That is, there's no such thing as a "value function" in C, right? Given that, what's the advantage for "your version" of the idiom? (This may just be nitpicking.)

Yes. Once declared, function pointers are similar to regular functions. They both point to a value that cannot be changed. That value is a block of code.

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

#58

Earlier quoted context omitted.

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.

Correct me if I'm wrong, but isn't a "function" always a pointer in C? That is, there's no such thing as a "value function" in C, right? Given that, what's the advantage for "your version" of the idiom? (This may just be nitpicking.)

No, but C will automatically take the address. The same goes for arrays. You can explicitly take the address of a function, and this is different from taking the address of a function pointer.

Pros: beginner confusion; sometimes bugs.

Cons: one less character to type; sometimes able to get more out of a macro

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

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

how do you remember a const pointer like > int * const a ? i.e. in what way do you extend your scheme such that it makes sense again?

'a' is a constant that can be dereferenced to get int.

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

#60

Earlier quoted context omitted.

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.

Correct me if I'm wrong, but isn't a "function" always a pointer in C? That is, there's no such thing as a "value function" in C, right? Given that, what's the advantage for "your version" of the idiom? (This may just be nitpicking.)

Functions degrade to pointers when passed as values, but they aren't inherently pointers (and these have distinct types, e.g., for C11 _Generic).
Post reply on HN