471,108 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

pinters to functions with variable arguments

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
Apr 20 '07 #1
6 2917
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?

--
Er*********@sun.com
Apr 20 '07 #2
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.

--Yan
Apr 20 '07 #3
In article <13*************@corp.supernews.com>,
CptDondo <ya*@NsOeSiPnAeMr.comwrote:
>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.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Apr 20 '07 #4
CptDondo wrote:
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 ...).

Apr 20 '07 #5
CptDondo wrote:
>
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:Th*************@gmail.com>

Apr 20 '07 #6
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
Apr 20 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
9 posts views Thread by Gibby Koldenhof | last post: by
5 posts views Thread by Sathyaish | last post: by
6 posts views Thread by Fuzzyman | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.