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, the easiest thing to do is to just use a typedef. You get to use the same syntax as for normal function calls with no trying to remember where that blasted extra * goes, and typically end up with cleaner code anyway. typedef void function_ptr_t(int i); function_ptr_t* ptr;
How Do I Declare a Function Pointer in C?
11–20 of 111 posts
Re: How Do I Declare a Function Pointer in C?
#12This 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.
Re: How Do I Declare a Function Pointer in C?
#13The trick to reading crazy C declarations is learning the "spiral rule": http://c-faq.com/decl/spiral.anderson.html (here are more examples, with nicer formatting: http://www.unixwiz.net/techtips/reading-cdecl.html )
Also this comment by Linus Torvalds (copy pasted because I don't know how to link to Google+ comments):
> I don't think that works. It breaks trivially for consecutive [] or * cases, something that he carefully didn't have in his examples.
> So the examples were made up to make it look like it's a spiral, but type parsing is about precedence, not about spirals. It so happens that the higher-precedence operators ([] and ()) are on the right-hand side, which is why it "works" to start on the right.
> And it doesn't explain why
> typedef int (fn_t[][2])(void);
> is ok, but
> typedef int (fn_t[2][])(void);
> is not.
> "Spirals"? I don't think so.
Re: How Do I Declare a Function Pointer in C?
#14mentions 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.Re: How Do I Declare a Function Pointer in C?
#15Re: How Do I Declare a Function Pointer in C?
#16The trick to reading crazy C declarations is learning the "spiral rule": http://c-faq.com/decl/spiral.anderson.html (here are more examples, with nicer formatting: http://www.unixwiz.net/techtips/reading-cdecl.html )
See also my comment last time this subject came up: https://news.ycombinator.com/item?id=12775966
Re: How Do I Declare a Function Pointer in C?
#17The trick to reading crazy C declarations is learning the "spiral rule": http://c-faq.com/decl/spiral.anderson.html (here are more examples, with nicer formatting: http://www.unixwiz.net/techtips/reading-cdecl.html )
As a variable:
returnType (*variableName)(parameterTypes) = function_name;
variableName is a pointer to a function that accepts parameterTypes and returns returnType As a static const variable:
static returnType (* const variableName)(parameterTypes) = function_name;
variableName is a const pointer to a function that accepts parameterTypes and returns static returnType (or maybe variableName is the static thing?) As an array:
returnType (*arrayName[])(parameterTypes) = {function_name0, ...};
arrayName is an array of pointers to functions that accept parameterTypes and return returnType As an argument to a function:
int my_function(returnType (*argumentName)(parameterTypes));
my_function is a function that accepts an argumentName, which is a pointer to a function accepting parameterTypes and returning returnType, and returns an int As a return value from a function:
returnType (*my_function(int, ...))(parameterTypes);
my_function is a function that accepts int and other parameters and returns a pointer to a function that accepts parameterTypes and returns returnType As a typedef:
typedef returnType (*typeName)(parameterTypes);
a typeName is now a pointer to a function that accepts parameterTypes and returns returnTypeRe: How Do I Declare a Function Pointer in C?
#18Re: How Do I Declare a Function Pointer in C?
#19Just use the typedef. Even if you personally find the other variants readable, chances are that your peer reading your code doesn't.
Re: How Do I Declare a Function Pointer in C?
#20Just 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
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.