How to pass varargs arguments to 2nd function?

A

antianti

Hello,

I have a function that has a variable # of arguments of varying types.

int foo(short a, ...)
{
va_list argp
va_start(argp, a);
...
va_end(argp);
}

I want to be able to call another function, offering the same
variable list of arguments to it.

int bar(int a, ...)
{
va_list argp
va_start(short, a);
...
va_end(argp);
}

How could such a thing be done?

The function bar does not have to use the ... expression
if not doing so will help.

Thanks.
 
H

Harald van Dijk

Hello,

I have a function that has a variable # of arguments of varying types.

int foo(short a, ...)
{
va_list argp
va_start(argp, a);
...
va_end(argp);
}

I want to be able to call another function, offering the same variable
list of arguments to it.

How could such a thing be done?

The function bar does not have to use the ... expression if not doing so
will help.

You can make bar accept a va_list argument, and have bar use va_arg on
foo's list.

int bar(va_list argp)
{
/* ... */
}

int foo(short a, ...)
{
va_list argp;
va_start(argp, a);
bar(argp);
va_end(argp);
}

Consider taking a look at the vprintf (and related) library functions.
They are specifically there to be called this way as well.
 
R

Robert Gamble

Hello,

I have a function that has a variable # of arguments of varying types.

int foo(short a, ...)
{
   va_list argp
   va_start(argp, a);
 ...
   va_end(argp);

}

I want to be able to call another function, offering the same
variable list of arguments  to it.

int bar(int a, ...)
{
   va_list argp
   va_start(short, a);
 ...
   va_end(argp);

}

How could such a thing be done?

The function bar does not have to use the ... expression
if not doing so will help.

Thanks.

<http://c-faq.com/> Question 15.12: <http://c-faq.com/varargs/
handoff.html>
 
A

antianti

I tried precisely what was in the FAQ and it didn't work with GNU CC.

Ah well, there is a solution: Pass a pointer to the location in the
stack where the varargs are located.
 
K

Kenny McCormack

I tried precisely what was in the FAQ and it didn't work with GNU CC.

Ah well, there is a solution: Pass a pointer to the location in the
stack where the varargs are located.

Beavis&Butthead: ork, ork, ork. He said "stack"...
 
B

Ben Bacarisse

I tried precisely what was in the FAQ and it didn't work with GNU
CC.

It does for me. Can you post an example of it not working? It is
possible there is a misunderstanding.
Ah well, there is a solution: Pass a pointer to the location in the
stack where the varargs are located.

Yuck. As a famous pig once said: "You're skate'n on thin ice, frog!".
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top