getting and passing variable number of parameters

M

Michael Cohen

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
(e-mail address removed)
 
J

Jack Klein

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
(e-mail address removed)

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
 
D

David M. Wilson

(e-mail address removed) (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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top