473,795 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass varargs arguments to 2nd function?

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.
Jul 27 '08 #1
6 29570
On Sun, 27 Jul 2008 13:58:00 -0700, antianti wrote:
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.
Jul 27 '08 #2
On Jul 27, 4:58*pm, antia...@rocket mail.com wrote:
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>

--
Robert Gamble
Jul 27 '08 #3

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.

Jul 28 '08 #4
In article <70************ *************** *******@m73g200 0hsh.googlegrou ps.com>,
<an******@rocke tmail.comwrote:
>
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"...

Jul 28 '08 #5
an******@rocket mail.com writes:
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 misunderstandin g.
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!".

--
Ben.
Jul 28 '08 #6
an******@rocket mail.com wrote:
I tried precisely what was in the FAQ and it didn't work with GNU CC.
Then either gcc has a rather serious bug, or your precision
isn't all it's cracked up to be. Not familiar with you but with
some experience of gcc, I incline toward the latter explanation.
Ah well, there is a solution: Pass a pointer to the location in the
stack where the varargs are located.
Oooog. There may be no such thing as the "worst possible"
solution, because any bad solution can most likely be made worse
by lavish application of additional error. "Worst possible" is
therefore a fictitious notion, something like the undescribable
singularity of a black hole. Our mathematics cannot cope with
the singularity, nor can our linguistic formalisms cope with
"worst possible." Still, we can safely say that your solution
is on the wrong side of the event horizon.

Oooog.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 28 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
18047
by: Vijay | last post by:
Hi all, Im using gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20) on 64bit linux server im trying to compile following code --------------------sam.cpp--------------------- #include <string> #include <iostream> #include <stdarg.h>
41
8343
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference?? Thanks, BB
5
11071
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer) modHandler.rptOpen (Me.Report.Name), (Me.Tag) End Sub
2
11902
by: Ian Partridge | last post by:
Hi, I want to write a varargs function which then passes its parameters to another varargs function. I have RTFFAQ: http://www.eskimo.com/~scs/C-faq/q15.12.html which says that the other function should accept a va_list pointer to do this. However, I can't seem to get this answer working.
9
4982
by: grid | last post by:
Hi, I have a function which takes a variable number of arguments.It then calls va_start macro to initialize the argument list.But here in my case I have a special builtin va_start provided by the compiler which I use for performance gains.Then the variable arguments are passed to another varargs function which then uses a builtin va_arg macro to get the arguments. Iam getting an error from the compiler in the called function when a local...
2
4128
by: Rick Anderson | last post by:
I want to store a "va_list" in a structure (which gets passed around from function to function). I cannot use the va_copy() routine to create a copy of the varargs (it looks like it uses stack memory). Has anyone written some portable code to create a copy of the varargs information, and subsequent routines to retrieve the varargs information? Thanks in advance! Rick
5
4593
by: metaperl.etc | last post by:
The following program does not work if you uncomment #lis = + list(args) Evidently Python is opting for the nullary constructor list() as opposed to the other one which takes a sequence. But no newcomer would know this. And the Python docs dont give a good example of dealing with taking a sequence of args and converting it to a list. I must've spent 40 minutes looking through my ora books, googling and irc'ing in vane on this.
5
3109
by: Gene | last post by:
Hello all. Is the initializing assignment below ANSI standard- conforming? Is there a way to have the prefix parameters to ... type checked in such an assignment? E.g. in this case int x in the typedef matched with int a of foo? Appreciate the help. ------------
13
4238
by: Tim H | last post by:
Goal: to provide the simplest call-site possible at the cost of upfront hackery. I have a function that essentially takes a variable list of arguments: func(const string &k0, unsigned long long v0, const string &k1, unsigned long long v1, etc) This is going to be called a LOT from code that is hand-written, so I really ant to simplify the job of calling this function.
0
10448
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.