DEV Community

Discussion on: Function Pointers

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Below, we declare a pointer to a function that is passed void and returns void:

void (*foo)();

No. In C, an empty parameter list means nothing is specified about what is passed to the function for backwards compatibility with K&R C. To specify that the function has no parameters, you need to do:

void (*f)(void);
Enter fullscreen mode Exit fullscreen mode

You should also mention that you can call a pointed to function explicitly since it occurs in a lot of code this way:

printf("%d  squared is %d\n", n, (*fptr1)(n));
Enter fullscreen mode Exit fullscreen mode

You should also mention that you can have a typedef of function; see here.

You should also mention that you can pass a pointer to function as a parameter without a typedef:

int compute(int (*operation)(int, int), int num1, int num2);
Enter fullscreen mode Exit fullscreen mode

even though it's generally recommended that you use a typedef to make things more readable.

Your select function doesn't return anything if the argument isn't either a + or -: you should handle that case.

Collapse
 
ahmedgouda profile image
Ahmed Gouda

Thanks very much for your comment, it is really valuable.