469,160 Members | 1,819 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

getting and passing variable number of parameters

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
2 7616
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
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.

Similar topics

3 posts views Thread by domeceo | last post: by
4 posts views Thread by Alfred Taylor | last post: by
26 posts views Thread by Michael McGarry | last post: by
17 posts views Thread by Charles Sullivan | last post: by
1 post views Thread by Darsin | last post: by
4 posts views Thread by Mike Dinnis | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.