473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 3183
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.