tags:

views:

221

answers:

3

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.

+1  A: 

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?

David Lively
They can be thought of that way, but in this declaration `double x[]` and `double *x` are equivalent.
Chris Lutz
No sir, that is not a homework question. I am not even a c programmer but am asked to review some c code.
leon
+1  A: 

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.

wallyk
I always preferred not to use `[]` in function parameters. `*argv[]` is more visually confusing than `**argv`, and if you use brackets new C programmers are liable to think that the variable is actually an array rather than a pointer, and attempt some evil `sizeof(x) / sizeof(x[0])`-type operation on it. Best (IMHO) to make it perfectly clear that it's not an array, and shouldn't be treated as one.
Chris Lutz
I was always in the minority advocating **argv. I'm glad to find another brother.
wallyk
+7  A: 

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)

John Knoeller
Not true about C++. The form is pefectly valid in both standard C and standard C++. There's absolutely nothing archaic about it. Function type in parameter declarations decays to ponter type in exactly the same fashion as array type in parameter declarations decays to pointer type. Both work in exactly the same way in C and C++ (aside from the meaning of `()` parameter list, which is beside the point in this case).
AndreyT
@AndreyT: odd then that you never see this form used, but I will change the answer
John Knoeller
`array[]` is legal for a variable when using an initialization list. `int array[] = {2, 3, 5, 7, 11, 13};`
wallyk
It is in C99 standard (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) Section 6.7.5.3 Function declarators (including prototypes)Constraint 8
Maxwell Troy Milton King
@wallyk: `array[]` is legal in any non-defining declaration in both C and C++. The only difference is that the size-less array type is *incomplete*.
AndreyT