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(),
void gradc()
)
note that fun() and gard() are passed as function. My question is if this is valid standard C grammar.
Thanks.
C has been standardized by ANSI since 1989 (ANSI C) and by the International Organization for Standardization (ISO). C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system.
So there are many compilers in C like BDS, Clang, GCC, Interactive C, Lattice, Portable C Compiler, Visual Express, etc.
What is C standard? The latest C standard is ISO/IEC 9899:2018, also known as C17 as the final draft was published in 2018. Before C11, there was C99. The C11 final draft is available here.
Type “gcc –version” in command prompt to check whether C compiler is installed in your machine. Type “g++ –version” in command prompt to check whether C++ compiler is installed in your machine.
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?
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