Are the two styles for defining a C function pointer different?

I accidentally caught myself using two different styles for defining C function pointers in different places and decided to write a minimal program to check for these differences.

These two styles:

int (comp (int)) 

and

 int (*comp)(int) 

I wrote a minimal program that uses both of these styles for the same function pointer, one to declare a function and one to define the same function, to see what the compiler has 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, I used the int (*a_fn)() style for the first parameter in the declaration, and I used the int (a_fn ()) style in the function definition. I am doing something similar with the following two arguments.

I suggested that these may be incompatible types, that these styles may be the difference between passing by reference and assigning a variable to a variable, but when I compiled this program, the compiler silently and happily compiled it, There are no errors, no warnings, nothing.

Since I saw so many textbooks on the second style, while I personally prefer the first style for aesthetic purposes, I am interested in what is the difference between the 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; } 
+8
c pointers function-pointers
source share
2 answers

In the parameter list, functions are converted to function pointers. Standard C11, ISO / IEC 9899: 2011, ยง6.7.6.3 Declaration of functions (including prototypes) says:

ยถ8 Declaring a parameter as "return type of function" should be adjusted to "pointer to return type of function" ...

So, as it is written, all parameters are treated as a "pointer to a function". It is best to be self-consistent.

Note that a function cannot return a function:

ยถ1 A function declaration must not indicate a return type, which is a function type or an array type.

Outside of the function declarator (a list of arguments to the function definition or function declaration), the notation is different:

 int (*a_fn)(); int (b_fn()); 

The first of them declares (defines) a variable named a_fn - a pointer to a function that returns an int with an indefinite (but not variational) list of arguments.

The second of them declares the existence of a function called b_fn , which returns an int with an indefinite (but not variable) list of arguments (and has an excessive set of parentheses, it can be written int b_fn(); and this means the same thing).

+4
source share

Both parameters int (comp (int)) and int (*comp)(int) equivalent when using the function as parameters. Function labels are converted to pointers to the function itself.

C11-ยง6.3.2.1:

A function designation is an expression that has a function type. _Alignof it is the operand of the sizeof operator, the _Alignof operator, 65) or the unary operator & , the designation of a function with the type of the return type of the function is converted to an expression that has the type "pointer to the return type of the function".

+2
source share

All Articles