I think it would be easier to use function pointers if I created a typedef for a function pointer, but I seem to be getting myself tripped up on some syntax or usage or something about typedef for function pointers, and I could use some help.
I've got
int foo(int i){ return i + 1;}
typedef <???> g;
int hvar;
hvar = g(3)
That's basically what I'm trying to accomplish I'm a rather new C programmer and this is throwing me too much. What replaces <???>
?
Your question isn't clear, but I think you might want something like this:
You are right. The function pointer can be conveniently used to point to the different functions of the same return type and taking same number of arguments. The argument types should match the declaration of the function pointer arguments.
In your case you could define your function pointer
g
as:typedef int (*g)(int);
//typedef
of the function pointer.g
is a function pointer for the function returningint
value and taking oneint
argument.The usage of function pointer could be illustrated by a simple program below:
OUTPUT:
The original way of writing the function returning function pointer is
Here
call
is a function which takes nothing but returns a function pointer which takes 2 arguments and returns an integer value. Pay attention to the brackets, they are absolutely necessary.Here is the code: