I have caught myself accidentally using two different styles of C function pointer definitions in different places and have decided to write a minimal program to test these differences.
The two styles in question are:
int (comp (int))
and
int (*comp)(int)
I wrote a minimal program that uses both of these styles for the same function pointer, one for the declaration of a function and one for the definition of the same function, to see what the compiler would have to say about it.
Declaration:
int a_or_b (int (*a_fn)(), int (b_fn()), int (*comp)(int));
Definition:
int a_or_b (int (a_fn()), int (*b_fn)(), int (comp(int)))
{
int out = a_fn ();
if (comp (out))
out = b_fn ();
return out;
}
As you can (hopefully) see, for the first parameter in the declaration I used the style int (*a_fn)(), while in the definition of the function, I used the style int (a_fn ()). I do something similar with the next two arguments.
I speculated that these might be incompatible types, that these styles might be the difference between a pass-by-reference and pass-by-value assignment of the variable, but when I compiled this program the compiler silently and happily compiled it. No errors, no warnings, nothing.
As I've seen very many tutorials advocating for the second style, while I personally prefer the first style for aesthetic purposes, I am interested in what the difference is between these two styles and which is recommended.
Full code example:
#include <stdio.h>
int a ();
int b ();
int a_or_b (int (*a_fn)(), int (b_fn()), int (*comp)(int));
int a ()
{
return 1;
}
int b ()
{
return 2;
}
int comparison (int param)
{
return param;
}
int a_or_b (int (a_fn()), int (*b_fn)(), int (comp(int)))
{
int out = a_fn ();
if (comp (out))
out = b_fn ();
return out;
}
int main (int argc, char **argv)
{
printf ("%i\n", a_or_b (a, b, comparison));
return 0;
}