Live data from Hacker News

How Do I Declare a Function Pointer in C?

fuckingfunctionpointers.com

81–90 of 111 posts

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

#81

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.

19 years as a C/C++/ObjC developer, and this never occurred to me. And it works with C Blocks! typedef int IntegerProcessor(int); int executeTheFunctionPointer(IntegerProcessor* function) { return function(23); } int executeTheBlock(IntegerProcessor^ block) { return block(32); } int doubler(int a) { return a * 2; } int main(int argc, const char * argv[]) { IntegerProcessor* myFunctionPtr = &doubler; int a = executeTh…

This is so cool. Where can I find documentation on the following?

   IntegerProcessor^ myBlock = ^(int b) {
      return a * b;
   };

I wasn't aware C supported nested functions.

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

#82

Earlier quoted context omitted.

19 years as a C/C++/ObjC developer, and this never occurred to me. And it works with C Blocks! typedef int IntegerProcessor(int); int executeTheFunctionPointer(IntegerProcessor* function) { return function(23); } int executeTheBlock(IntegerProcessor^ block) { return block(32); } int doubler(int a) { return a * 2; } int main(int argc, const char * argv[]) { IntegerProcessor* myFunctionPtr = &doubler; int a = executeTh…

This is so cool. Where can I find documentation on the following? IntegerProcessor^ myBlock = ^(int b) { return a * b; }; I wasn't aware C supported nested functions.

https://en.wikipedia.org/wiki/Blocks_(C_language_extension)

Probably, you can't use it.

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

#83
post #78

Every time I have to deal with the declarator syntax in C or C++, I can't help but ponder what K&R were thinking when they designed this. It's not like there weren't other languages back then with a saner approach. It looks like what they did was take the syntax from B: auto x[10]; and generalize it such that the type name ended up before the variable name, as in Algol. But in B this worked much better, because it di…

What they were thinking can be see in K&R C - there is no "typedef" in early C. Without typedef, the syntax of C is context-independent and LALR-1. You don't have to know if a name is a type to parse the syntax. Then came "typedef", which broke parsing. C parsing became context-dependent. To parse C with "typedef", and especially C++, you must read all the header files first.

With name-first declaration syntax (Pascal, Modula, Go, Rust), parsing is context-independent again. Readability improves. Error messages improve. Syntax-coloring editors with a single-file view don't get lost.

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

#84

Earlier quoted context omitted.

19 years as a C/C++/ObjC developer, and this never occurred to me. And it works with C Blocks! typedef int IntegerProcessor(int); int executeTheFunctionPointer(IntegerProcessor* function) { return function(23); } int executeTheBlock(IntegerProcessor^ block) { return block(32); } int doubler(int a) { return a * 2; } int main(int argc, const char * argv[]) { IntegerProcessor* myFunctionPtr = &doubler; int a = executeTh…

This is so cool. Where can I find documentation on the following? IntegerProcessor^ myBlock = ^(int b) { return a * b; }; I wasn't aware C supported nested functions.

Its objective c fuckface

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

#86
post #68

Earlier quoted context omitted.

I'd say this is even more readable: auto get_func_on() -> std::function

It's more readable, but using std::function here introduces a second layer of indirection vs using a plain function pointer. More specifically, std::function's operator() is virtual, and calls into a subclass that's specialized to function pointers of type void(int). The subclass then performs the actual function pointer call.

Technically function::operator() is not virtual (which wouldn't be very useful as std::function has value semantics), but it does runtime dispatching internally using an unspecified mechanism.

This can be virtual functions, or, more commonly, an hand rolled vtable. In the last case, if std::function is constructed with a function pointer exactly matching its signature it could in principle avoid the thunk and directly point to the function itself. I don't think most implementations bother.

/pedantic

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

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

I agree that typedefs are clearer for this but usually you don't have a choice and neither does the reviewer. E.g. coding style for Linux essentially says no typedefs. On the upside I have worked on projects were it's required.

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

#88
post #79
post #78

Every time I have to deal with the declarator syntax in C or C++, I can't help but ponder what K&R were thinking when they designed this. It's not like there weren't other languages back then with a saner approach. It looks like what they did was take the syntax from B: auto x[10]; and generalize it such that the type name ended up before the variable name, as in Algol. But in B this worked much better, because it di…

What if I told you that even declarations like char const *(* const (*(*ip)())[])[] are trivial to read? [Read this]( http://www.icce.rug.nl/documents/cplusplus/cplusplus03.html#... ) and you'll never struggle with any declaration ever again.

I know the rules, and how to apply them. But they are not trivial. The fact that such documents need to be written in the first place, and the fact that utilities like cdecl exist, is a strong testimony to that.

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

#89
post #83
post #78

Every time I have to deal with the declarator syntax in C or C++, I can't help but ponder what K&R were thinking when they designed this. It's not like there weren't other languages back then with a saner approach. It looks like what they did was take the syntax from B: auto x[10]; and generalize it such that the type name ended up before the variable name, as in Algol. But in B this worked much better, because it di…

What they were thinking can be see in K&R C - there is no "typedef" in early C. Without typedef, the syntax of C is context-independent and LALR-1. You don't have to know if a name is a type to parse the syntax. Then came "typedef", which broke parsing. C parsing became context-dependent. To parse C with "typedef", and especially C++, you must read all the header files first. With name-first declaration syntax (Pasca…

That explains how they ended up with context-dependent syntax - you're right, in the absence of typedefs, it's context-free to parse because of tags. But it can still be a pain to read, if you run into things like arrays of pointers to functions.

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

#90
post #78

Every time I have to deal with the declarator syntax in C or C++, I can't help but ponder what K&R were thinking when they designed this. It's not like there weren't other languages back then with a saner approach. It looks like what they did was take the syntax from B: auto x[10]; and generalize it such that the type name ended up before the variable name, as in Algol. But in B this worked much better, because it di…

The idea was that the syntax for declaring a data type mirrors the syntax for using that datatype. See https://www.bell-labs.com/usr/dmr/www/chist.pdf for Ritchie's own comments.

It was not a successful idea, but there was a method to the madness.

Post reply on HN