www.thecareerplus.com
The CareerPlus
Home Technical Resources Programming Functions
Thursday 09th September 2010
 
 
How can I pass the variable argument list passed to one function to another function.

Discuss it!          

Something like this wont 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);
}


This is 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); 
  }
}

Discuss it!          

CrackTheIntervew.NET
Advertisement
 
Top! Top!