I have a series of functions with the same prototype, say
int func1(int a, int b) { // ... } int func2(int a, int b) { // ... } // ...
Now, I want to simplify their definition and declaration. Of course I could use a macro like that:
#define SP_FUNC(name) int name(int a, int b)
But I'd like to keep it in C, so I tried to use the storage specifier typedef
for this:
typedef int SpFunc(int a, int b);
This seems to work fine for the declaration:
SpFunc func1; // compiles
but not for the definition:
SpFunc func1 { // ... }
which gives me the following error:
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
Is there a way to do this correctly or is it impossible? To my understanding of C this should work, but it doesn't. Why?
Note, gcc understands what I am trying to do, because, if I write
SpFunc func1 = { /* ... */ }
it tells me
error: function 'func1' is initialized like a variable
Which means that gcc understands that SpFunc is a function type.
A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.
While a function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, i.e. what data types go in and come out of it.
A typedef declaration is a declaration with typedef as the storage class. The declarator becomes a new type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared.
A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.
You cannot define a function using a typedef for a function type. It's explicitly forbidden - refer to 6.9.1/2 and the associated footnote:
The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition.
The intent is that the type category in a function definition cannot be inherited from a typedef:
typedef int F(void); // type F is "function with no parameters // returning int" F f, g; // f and g both have type compatible with F F f { /* ... */ } // WRONG: syntax/constraint error F g() { /* ... */ } // WRONG: declares that g returns a function int f(void) { /* ... */ } // RIGHT: f has type compatible with F int g() { /* ... */ } // RIGHT: g has type compatible with F F *e(void) { /* ... */ } // e returns a pointer to a function F *((e))(void) { /* ... */ } // same: parentheses irrelevant int (*fp)(void); // fp points to a function that has type F F *Fp; //Fp points to a function that has type F
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With