pinters to functions with variable arguments

C

CptDondo

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?

--Yan
 
E

Eric Sosman

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?
 
C

CptDondo

Eric said:
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.

--Yan
 
W

Walter Roberson

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 `...'

There is no way to declare a pointer to functions that each
take fixed arguments but that the number of arguments vary.
The use of ... is reserved for functions which have been
declared with ... and which use the stdarg facilities to access
their arguments.

What you need to do for your pointer is to pick one particular
declaration, such as void (*func)(void) [but any fixed declaration
could be used]. Then, when you want to pass other function
pointers in to that routine, cast the function pointer type
in order to pass it in, and when you need to use that pointer
in your routine, cast it back to the actual type before the call.
C promises that you can cast function pointer types to each
other, and that you will be able to use the pointer as long as
you cast the pointer back to the original type.


For example,

[...]

void invoke_me( void (*func)(void), int function_class ) {
int row = 7, col = 15, ln = 42;

if (function_class == 1)
(*(void (*)(int,int))func)(row,col)
else if (function_class == 2)
(*(void (*)(int))func)(ln)
}

int main(void) {

invoke_me( (void (*)(void))tc_cm, 1 );
invoke_me( (void (*)(void))tc_DO, 2 );

return 0;
}

So in the calling routine you cast the pointer type to pass it in,
and then when you want to use the pointer, you cast it back to
its proper type and use that call the function.
 
G

Guest

CptDondo said:
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?

You can use void (*func)(); if you don't mind that a) you cannot pass
chars, shorts, or floats, and b) you cannot use it if any of the
functions actually take a variable number of arguments (in other
words, if the function declaration uses ...).
 
K

Kenneth Brody

CptDondo said:
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 `...'
[...]

You're not using "functions with variable arguments". Rather, you
want a pointer to functions with "unspecified arguments". In that
case, I believe you can use:

void (*func)();

However, are you sure you want to do this? Why do you want/need a
single pointer to point to different types of functions like this?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
E

Eric Sosman

CptDondo wrote On 04/20/07 12:27,:
Eric said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top