Live data from Hacker News

How Do I Declare a Function Pointer in C?

fuckingfunctionpointers.com

71–80 of 111 posts

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

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

My brain parses both of your expressions (and both of your comments) the same way.

Like, it's tautological: 'de-referencing an int pointer will give you an int'

Brains are weird.

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

#74
post #71
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.

My brain parses both of your expressions (and both of your comments) the same way. Like, it's tautological: 'de-referencing an int pointer will give you an int' Brains are weird.

Mine does too, but that's because I've trained myself to see it that way in order to understand this exact concern.

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

#75

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

I would say that a "function pointer" isn't really a pointer: you can't dereference it or do arithmetic on it. Really, the things we currently call "functions" should be "function literals", and then what's now a "function pointer" could be just a "function".

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

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

Another advantage of typedefing the function is that you can let the compiler help ensure your function definition signatures are correct.

For example, if you typedef a callback type like so:

  typedef int callback_t(int foo);
And then declare a callback like so:

  callback_t my_callback;
And then later define the callback like so:

  int my_callback(int foo) {
    ...
  }
The compiler will produce an error if you screw up the function signature of my_callback() when defining it because it won't match the prototype you defined via the typedef. This only works in C. C++ allows multiple function signatures for the same function name, so you probably won't get a compiler error--though you'll likely wind up with a linker error.

Edit: The downside is that function declarations done this way will look a little odd--possibly mistaken for a variable definition. And, upon further thought, I'm not sure that this pattern really is a huge benefit since function pointer assignment will also produce an error if the signatures don't match. But interesting nonetheless, I guess.

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

#77

Earlier quoted context omitted.

One advantage is that you can declare functions with it, which is useful when you have many different operations of the same type. For example, take a toy calculator: typedef double binary_operation(double, double); binary_operation add, subtract, multiply, divide; double add(double a, double b) { return a + b; } /* ... */ struct binary_operator { char const *name; binary_operation *operation; } binary_operators[] =…

Well, yeah, obviously there's less redundancy, but I'm specifically not seeing the advantage that OP mentioned. (Which is all that I'm questioning.)

Here's an example:

    typedef void (*sighandler_t)(int);
    sighandler_t signal(int signum, sighandler_t handler);
Without the typedef, it's much less clear.

Edit: It could instead be written as the non-pointer type:

    typedef void (sighandler_t)(int);
And then used as:

    sighandler_t *signal(int signum, sighandler_t *handler);
And as a function declaration:

    sighandler_t foo;
With the pointer in the typedef, the type can't be used to declare functions.

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

#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 didn't have array types (or pointer types, or function types) - everything was a machine word. So [] in a variable declaration was just to allocate memory to which the variable would refer; the variable itself would still be a word. When they made [] part of the type, and added pointers and function types, the result was a mess.

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

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

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

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

"Unambiguous" is not the same as "trivial". C code generally reads left to right, so a trivial syntax for declaring, say, an array of const pointers to functions that take a const pointer to a char and return a const pointer to a char, would have tokens for those things in that order.
Post reply on HN