Live data from Hacker News

How Do I Declare a Function Pointer in C?

fuckingfunctionpointers.com

61–70 of 111 posts

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

#61

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

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[] = {
        { "+", add },
        { "-", subtract },
        { "*", multiply },
        { "/", divide },
        { NULL, NULL },
    };
You can also use the function type in parameter lists, but it’s equivalent to a function pointer type.

    int atexit(void function(void));

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

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

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 = executeTheFunctionPointer(myFunctionPtr);
        printf("%d\n", a); // 46

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

        a = executeTheBlock(myBlock);
        printf("%d\n", a); // 1472

        return 0;
    }
My mind is completely blown.

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

#63
post #49

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.

It's never even occurred to me to typedef a function like this, and now that I think about it I'm not sure why. Your way is a lot clearer. Thanks for the tip. Do you remember where you picked this up? Any particular book or codebase?

I picked it up just reading various C stuff on the internet - I think this one actually came from a Reddit user. It's not a use-case I think I've ever seen used anywhere, except for my personal code. Even then though, I admittedly almost always just type out the regular function-pointer syntax, since I find that function-pointers for me almost always just get declared in one location anyway.

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

#64

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…

So is mine.

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

#65

Earlier quoted context omitted.

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

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

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

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

Shitty corporate ones. I used to work at a place that filtered the content too, so this page would be blocked by your post.

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

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

You don't think perhaps a filter might assume that a website whose URL contains "fuck" might have something to do with porn?

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

#68

The new c++ alt function syntax talked about here: https://blog.petrzemek.net/2017/01/17/pros-and-cons-of-alter... mentions replacing function declarations for void (*get_func_on(int i))(int); with auto get_func_on(int i) -> void (*)(int); which looks a lot more readable to me.

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.

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

#69

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

This is very similar to the 'array-type', which I've written about before (And could talk about if you're interested). Functions (and arrays) degrade into a pointer to themselves when used in most situations (for functions, I can't think of a real case where it doesn't degrade, besides declarations). But the 'func' in this case is the type of a 'function' ('value function' as you're referring). Interestingly, declaring something of type 'func' is the same as forward declaring a function of that type:

    typedef int func(void);

    /* These two lines are equivalent */
    func foo;
    int foo(void);
Obviously though, the above is of limited usefulness. It is kinda handy to ensure functions are compatible with a certain typedef, but if it isn't you'll generally see warnings or errors in other locations anyway.

The advantage of my technique here is that it doesn't 'hide' the pointer inside of the typedef, which I consider poor form. Consider these two:

    typedef int type1;
    typedef int type2(void);
    typedef int (*type3)(void);

    type1 *var1;
    type2 *var2;
    type3  var3;
    type3 *var4;
All of the above are actually pointers, but from the declaration alone you can't tell that `type3 var3` actually declares a pointer. In fact, `type3 var3` and `type2 * var2` declare the exact same thing (minus the name), but `type2 * var2` makes it clear that `var2` is a pointer and not a value type. I find this to be a fairly nice aide in reading, and if you keep this consistent for all types then you don't ever have to worry about a pointer being hidden, or the usage not matching the declaration (IE. You declare it as `type3 var3` but then do something like `* var3`, which looks incorrect unless you know `type3` is actually a pointer).

Moreover, lots of people that are newer to C (and even those that aren't but just aren't clear or didn't fully check what `type3` is) will attempt to use `type3 * var`, a double-pointer to a function, when what they really want is `type3 var`, a pointer to a function. There's no confusion over what it is if you don't hide the pointer in the first place, and there's really no great reason to hide it besides not knowing you can avoid hiding it in the first place. Even when you're not new to C, keeping track of things when people do `typedef struct foo * bar` and then `bar * foo2` can get to be a headache really fast.

Edit: Fixed formatting

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

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

Considering that

    int * foo, bar;
are variables of two different types, the asterisk clearly has affinity to the variable name. It is misleading to bind it to the base type name.
Post reply on HN