Friday, Jan 19th

Last update12:59:40 PM GMT

How can I pass the variable argument list passed to one function to another function?

Write e-mail

For starters, something like this will not work

#include <stdarg.h>
main()
{
 display("Hello", 4, 12, 13, 14, 44);
}

display(char *s,...)
{
 show(s,...);
}

show(char *t,...)
{
 va_list ptr;
 int a;
 va_start(ptr,t);
 a = va_arg(ptr, int);
 printf("%f", a);
}

The right way of doing it
#include <stdarg.h>
main()
{
 display("Hello", 4, 12, 13, 14, 44);
}

display(char *s,...)
{
 va_list ptr;
 va_start(ptr, s);
 show(s,ptr);
}

show(char *t, va_list ptr1)
{
 int a, n, i;
 a=va_arg(ptr1, int);

 for(i=0; i<a; i++)
 {
 n=va_arg(ptr1, int);
 printf("\n%d", n); 
 }
}
argaiv1077

Share this post



Web Hosting