I am not familiar with the syntax below for the following struct
struct fp {
int (*fp)();
}
What is int (*fp)()
? I understand that it is an integer
and *fp
is a pointer but I do not understand what that parentheses in (*fp)()
do.
I am not familiar with the syntax below for the following struct
struct fp {
int (*fp)();
}
What is int (*fp)()
? I understand that it is an integer
and *fp
is a pointer but I do not understand what that parentheses in (*fp)()
do.
fp
is a pointer to a function with empty parameters list.
I.e.
int myfunc() //define function
{
return 0;
}
struct fp //define structure
{
int (*fp)();
} mystruct;
mystruct.fp = &myfunc; //assign function pointer to structure element
int a = mystruct.fp(); //Call function through pointer
There are many ways to read C declarations, that can be very complicate in some cases. Start reading this https://parrt.cs.usfca.edu/doc/how-to-read-C-declarations.html.
You can google for "how to read c declarations" for more in depth explanation and further tips.
As Swordfish pointed out, the use of empty parameter list imply other sensible points about function definitions, that could be worth to deep. Please refer to Swordfish's comment below for the main points relative to functions definition.
I will refer only to the §6.11.6 Function declarators (of §6.11 Future language directions):
The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
It is a function pointer. They are powerful and quite hard to get your head around if you are a beginner.
It is a declaration of the variable fp
which is a pointer to a function that returns an int and takes an unspecified list of arguments.
It is a strcture that wraps a function-pointer.
Have a look here: How do function pointers in C work?