I've been reading a c code here. I don't understand this typedef:
typedef const char * (*Responder)( int p1);
Is p1 a parameter of Responder? Is Responder a const char?
typedef is (syntactically) nothing else than any other storage-class specifier (like static or extern), just think about what this would declare without the typedef:
const char * (*Responder)( int p1);
Responderis a pointer to a function (getting anint) returning a pointer toconst char.
So, with the typedef, you have:
Responderis a type synonym for a pointer to a function (getting anint) returning a pointer toconst char.
Related: http://c-faq.com/decl/spiral.anderson.html
HTH
Using typedef with a function pointer is definitely doable, as shown here and here.
For example (from one of the links):
int do_math(float arg1, int arg2) {
return arg2;
}
int call_a_func(int (*call_this)(float, int)) {
int output = call_this(5.5, 7);
return output;
}
int final_result = call_a_func(&do_math);
This code can be rewritten with a typedef as follows:
typedef int (*MathFunc)(float, int);
int do_math(float arg1, int arg2) {
return arg2;
}
int call_a_func(MathFunc call_this) {
int output = call_this(5.5, 7);
return output;
}
int final_result = call_a_func(&do_math);
In your specific case, it's a pointer to a function called Responder that returns a char * and takes an int as an argument.
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