Is this valid standard c?
I am reviewing some optimisation libraries and came across the function signature
double solvopt(unsigned short n,
double x[],
double fun(),
void grad(),
double options[],
double func(),
v开发者_如何学Gooid gradc()
)
note that fun() and gard() are passed as function. My question is if this is valid standard C grammar.
Thanks.
The use of double fun()
rather than double (*fun)()
is an archaic form, that was only valid in standard C and never in C++, and if I remember correctly, only when declaring a function argument. (much like ary[]
which is legal for a function argument, but not for an uninitialized variable)
Since it isn't possible (in C) to pass a function by value to another function, the compiler just took double fun()
to mean a pointer to a function that returned a double.
So this is valid (but archaic. has fallen out of favor)
func()
and gradc()
are in the proper form for functions with unknown parameters. I'm pretty sure this was an acceptable syntax even for Unix 6 circa 1975.
The []
parameters are the same as *
, now and in the past. In fact, I remember a 1980s dispute over which was more proper:
int main (int argc, char **argv, char **envp)
or
int main (int argc, char *argv[], char *envp[])
There is no different in effect. The dispute is which is more correct according to semantics.
Your array parameters (x and options) should probably be pointers.
Referring to a function with parenthesis, like "func()" or "gradc()" calls the function. The name of the function alone is a code pointer that can be dereferenced to call the function in question.
When in doubt, try compiling this with an ANSI C compiler - a lot of compilers provide an ANSI compatibility switch to enforce standards compliance. Also, the K&R book is your friend.
Is this a homework question?
精彩评论