CptDondo wrote On 04/20/07 12:27,:
Eric Sosman wrote:
>>CptDondo wrote On 04/20/07 12:03,:
>>>How do you declare a function with 0 or mroe arguments?
I have a bunch of functions like this:
void tc_cm(int row, int col);
void tc_do(void);
void tc_DO(int ln);
and I am trying to declare a pointer to them:
void (*func)( ... );
But this fails with :
error: ISO C requires a named argument before `...'
I've tried
void (*func)( [ ... ] );
and I get a syntax error.
I've tried various other mixes, but it seems that at least one argument
is required?
At least one named argument is required. Inside
the function, you need to mention that argument -- by
name -- when you use the va_start macro to begin
processing the unnamed variable arguments.
What is the larger problem you're trying to solve?
I was trying to provide a bit of code clarity, but I don't think that's
going to happen...
I can achieve the same thing by using a global struct....
Thanks for the clarification.
Maybe you've thanked me too soon ... Re-reading your
question, I see that I fixated on "variable arguments" and
didn't stop to examine what you were actually trying to do.
My answer was correct (a variadic function must have at
least one named argument), but wasn't really relevant.
What you're after is not a pointer to a function that
takes a variable number of arguments, but a pointer that
can refer to an assortment of functions whose argument
lists and return types don't necessarily match. On this
matter, there is good news and bad news.
The good news is that any function pointer variable
can be made to point to any function at all. If the type
of the pointed-to function doesn't agree with that of the
pointer you will need to convert it with a cast when
assigning or initializing:
int func1(void);
double func2(double x, double y);
void (*fptr)() = (void (*)())func1;
...
fptr = (void (*)())func2;
The bad news is that you can only call a function via
a pointer whose type agrees with the function's type. In
the above, you could not just write `i = fptr()' to call
func1, nor could you use `z = fptr(x,y)' to call func2.
Instead, you would have to "know" (somehow) what kind of
function fptr was aiming at, and convert it back to a
pointer of the proper type:
i = ((int (*)(void))fptr) ();
...
z = ((double (*)(double,double))fptr) (x, y);
Both the examples above are on the hard-to-read side,
and I'd recommend introducing a few typedefs to clarify
things. For example:
typedef void (*AnyFunc)(); /* "generic" func ptr */
typedef int (*IntOfVoid)(void); /* int f(void) ptr */
typedef double (*DblOfDblDbl)(double, double);
...
int func1(void);
double func2(double x, double y);
AnyFunc fptr = (AnyFunc)func1;
...
i = ((IntOfVoid)fptr)();
...
fptr = (AnyFunc)func2;
...
z = ((DblOfDblDbl)fptr) (x, y);
You could even use this technique with actual variable-
argument functions, as in:
typedef int (*IntOfStrVar)(const char*, ...);
...
fptr = (AnyFunc)printf;
...
((IntOfStrVar)fptr) ("Hello, cloud %d!\n", 9);
I hope this helps -- more than my first answer, anyhow.
--
Er*********@sun.com