By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,362 Members | 3,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,362 IT Pros & Developers. It's quick & easy.

Passing va_list two times?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

"Andrej Prsa" <an*********@guest.arnes.si> wrote in message
news:20040304020328.07906a44.an*********@guest.arn es.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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.