472,958 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 3141
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
9
by: Gibby Koldenhof | last post by:
Hiya, Terrible subject but I haven't got a better term at the moment. I've been building up my own library of functionality (all nice conforming ISO C) for over 6 years and decided to adopt a...
1
by: S?ren Gammelmark | last post by:
Hi I have been searching the web and comp.lang.c of a method of using variable-argument function pointers and the like. And some of the questions arising in this post are answered partly in...
5
by: Sathyaish | last post by:
I knew this but I have forgotten. How do you declare a function that has to accept a variable number of arguments? For instance, the printf() function has a variable number of arguments it can...
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
6
by: Fuzzyman | last post by:
Hello all, I'm trying to extract the code object from a function, and exec it without explicitly passing parameters. The code object 'knows' it expects to receive paramaters. It's 'arg_count'...
2
by: Alan | last post by:
I have a couple of questions about using a variable number of arguments in a function call (...). The context is that I have some mathematical functions I created. I currently pass them a pair of...
9
by: oldyork90 | last post by:
I'm going thru code and have never seen this before http://www.webreference.com/programming/javascript/mk/column2/3.html Look at function CreateDragContainer() on line 25. It has no arguments...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.