473,799 Members | 3,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing va_list two times?

Hi!

If I declare two functions like this:

int execute_command (char *name, ...)
{
va_list args;

va_start (args, name);
my_func (1, args);
va_end (args);
}

int my_func (int argc, ...)
{
va_list args;
char *str;

va_start (args, argc);
str = va_arg (args, char *);
printf ("%s\n", str);
va_end (args);
}

and call them from e.g.

execute_command ("my_command ", "my argument");

parsing of args is OK at the first function (e.g. if I cut & paste the
va_arg and printf part to the first function), but garbage is printed in
the second function. I'm surely missing something obvious, but I can't see
it. Help?

Thanks,

Andrej
Nov 14 '05 #1
2 4724

"Andrej Prsa" <an*********@gu est.arnes.si> wrote in message
news:2004030402 0328.07906a44.a n*********@gues t.arnes.si...
Hi!

If I declare two functions like this:

int execute_command (char *name, ...)
{
va_list args;

va_start (args, name);
my_func (1, args);
va_end (args);
}

int my_func (int argc, ...)
{
va_list args;
char *str;

va_start (args, argc);
str = va_arg (args, char *);
printf ("%s\n", str);
va_end (args);
}

and call them from e.g.

execute_command ("my_command ", "my argument");

parsing of args is OK at the first function (e.g. if I cut & paste the
va_arg and printf part to the first function), but garbage is printed in
the second function. I'm surely missing something obvious, but I can't see
it.


Um, 'char*' and 'int' are different types?
(taking literally your remark about cut/paste)

int i;

/* ... */

i = va_arg(args, int);

cut/copy/paste is a double-edged sword. :-)

-Mike
Nov 14 '05 #2
in comp.lang.c i read:
int execute_command (char *name, ...)
{
va_list args;

va_start (args, name);
my_func (1, args);
va_end (args);
}

int my_func (int argc, ...)
args is a va_list, not a ..., so you cannot do this. make my_func's second
argument a va_list, drop the va_start and va_end, and use vprintf.
parsing of args is OK at the first function (e.g. if I cut & paste the
va_arg and printf part to the first function), but garbage is printed in
the second function.


didn't your compiler whine? if not you need to increase the warning level,
and if it did you needed to pay attention to what it said.

--
a signature
Nov 14 '05 #3

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

Similar topics

3
28239
by: Shea Martin | last post by:
I have a function: void f(char* format, ...) { va_list args; char buf; sprintf(buf, format, args); printf(buf); }
2
11903
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.
2
7819
by: Michael Cohen | last post by:
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 ,...)
1
9476
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use something like %V to pass in another format string and a va_list to allow nesting. It happens to work on my compiler, but I wasn't sure if it's portable to use va_list's as parameters to a variable argument function because va_list isn't always just a...
4
19250
by: Tor Rustad | last post by:
I have a C program, where I control the error behavior according to context: /* some error handlers */ static void on_error_log (MYSQL *mysql, const char *msg, ...); .... static void on_error_exit (MYSQL *mysql, const char *msg, ...); /* the error function */ static void (*error_fatal) (MYSQL *mysql, const char *msg, ...) =
6
2479
by: junky_fellow | last post by:
Hi, I want to define a function log_file(), that would print the formatted message to stdout if the FILE pointer passed is NULL else print the formatted output to the file corresponding to FILE*. For eg. extern FILE *fp;
5
4669
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote: The relevant paragraph from the Standard is: ---- Begin Quote ---- The type declared is va_list which is an object type suitable for holding information needed by the macros
6
4068
by: Laurent Deniau | last post by:
When I compile the code below with gcc -std=c99 -W -Wall -pedantic -O3 -Winline, it reports the following: variadic.c: In function ‘fv’: variadic.c:12: warning: function ‘fv’ can never be inlined because it uses variable argument lists variadic.c: In function ‘vf’: variadic.c:12: warning: inlining failed in call to ‘fv’: function not inlinable variadic.c:27: warning: called from here
1
5551
by: Chuck Chopp | last post by:
I have some code that is being built on the following: Windows Server 2003, both 32-bit & 64-bit editions Windows Vista, both 32-bit & 64-bit editions Windows Server 2008, both 32-bit & 64-bit editions Build tools: Visual Studio 2008. SLES 10 SP1, both 32-bit & 64 bit editions
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10247
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...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
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
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
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
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
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.