By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

getting and passing variable number of parameters

P: n/a
Hi,
I have to write a function that gets variable number of parameters and
pass them to another function (existing).
The problem is that the prototype of the function I call has ",..." so
I can't just pass it parameters in the format of va_list.

The following code does not work since f2 doesn't take arg_list (the
prototype of f2 is the same as f1's)

void f1(char a ,...)
va_list arg_list;
va_start (arg_list, a);
f2(a,arg_list); <==== f2 doesn't take arg_list
va_end;

any ideas how I can do it?
Thanks,
Michael
co***********@yahoo.com
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 25 Dec 2003 03:32:30 -0800, co***********@yahoo.com (Michael Cohen)
wrote in comp.lang.c:
Hi,
I have to write a function that gets variable number of parameters and
pass them to another function (existing).
The problem is that the prototype of the function I call has ",..." so
I can't just pass it parameters in the format of va_list.

The following code does not work since f2 doesn't take arg_list (the
prototype of f2 is the same as f1's)

void f1(char a ,...)
va_list arg_list;
va_start (arg_list, a);
f2(a,arg_list); <==== f2 doesn't take arg_list
va_end;

any ideas how I can do it?
Thanks,
Michael
co***********@yahoo.com


There is no way to do this using standard C. You will need to consult
a compiler-specific group to see if you can hack up some compiler
specific solution.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #2

P: n/a
co***********@yahoo.com (Michael Cohen) wrote...
Hi,
I have to write a function that gets variable number of parameters and
pass them to another function (existing).
The problem is that the prototype of the function I call has ",..." so
I can't just pass it parameters in the format of va_list.


What you are trying to do is not possible using standard C. You have
two options, one of which has already been suggested. The other is
this:

If you have source access to the function accepting variadic arguments
that you would like to call, renaming it and modifying it's prototype
may be a better idea. For example:

void mylog(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* ... code ... */
va_end(ap);
}
To make this function callable with variable arguments at runtime from
other functions, you could do something like:

void vmylog(const char *fmt, va_list ap)
{
/* ... code ... */
}

void mylog(const char *fmt, ...)
{
va_list ap;
va_start(ap);
vmylog(fmt, ap);
va_end(ap);
}
You can now simply pass vmylog a va_list when you please. If you have
not got source access to the procedure you wish to call, then you have
to go non-portable, otherwise you are screwed.
David.
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.