vprintf to printf

Reply

Join Date: Feb 2008
Posts: 1
Reputation: awelex is an unknown quantity at this point 
Solved Threads: 0
awelex awelex is offline Offline
Newbie Poster

vprintf to printf

 
0
  #1
Feb 5th, 2008
Hi,

I'm new here, so please be gentle
For an application I'm working on I need to implement a callback function that takes a va_list as an argument. Inside that function, however, I need to pass the va_list (or rather all arguments of the callback function) to a function with a printf-like syntax (i.e. not a va_list). I was thus wondering if there's a way to "convert" a va_list so that it can be used with a printf-like function. Obviously, I could use vsprintf to write everything into one string first, but that would require quite a large buffer. Is there another way to do it?

Thanks,
Alex
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 58
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: vprintf to printf

 
0
  #2
Feb 6th, 2008
hi
i think u r talking about variable no. of argument for that u can use stdarg.h
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,636
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: vprintf to printf

 
0
  #3
Feb 6th, 2008
>I was thus wondering if there's a way to "convert" a
>va_list so that it can be used with a printf-like function.
No, a variable argument list and a va_list object are directly equivalent to an object of type T and a pointer to type T, respectively. The types are incompatible, and while you can convert an object of type T into a pointer to type T using the & operator (or a variable argument list to a va_list object using the va_start macro), you can't assign a pointer to type T to an object of type T (or a va_list object to a variable argument list). Because of that, while this may compile, the behavior is undefined (where "undefined" in this case usually means it's not going to work):
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3.  
  4. void foo ( int n, ... );
  5. void bar ( int n, va_list args );
  6. void baz ( int n, ... );
  7.  
  8. int main ( void )
  9. {
  10. foo ( 4, "this", "is", "a", "test" );
  11. return 0;
  12. }
  13.  
  14. void foo ( int n, ... )
  15. {
  16. va_list args;
  17.  
  18. va_start ( args, n );
  19. bar ( n, args );
  20. va_end ( args );
  21. }
  22.  
  23. void bar ( int n, va_list args )
  24. {
  25. baz ( n, args );
  26. }
  27.  
  28. void baz ( int n, ... )
  29. {
  30. va_list args;
  31.  
  32. va_start ( args, n );
  33. while ( --n >= 0 )
  34. printf ( "%s\n", va_arg ( args, char* ) );
  35. va_end ( args );
  36. }
If you have the power to do so, the best solution is the one that the standard library uses. For each variadic function, supply a corresponding function that takes a va_list. That's a trivial solution because there's no repetition (the variadic function can be implemented in terms of the va_list function), but it does require that the va_list function do all of the work. For example, here's a reasonable implementation of printf and vprintf:
  1. int printf ( const char *fmt, ... )
  2. {
  3. va_list args;
  4. int rv;
  5.  
  6. va_start ( args, fmt );
  7. rv = vprintf ( fmt, args );
  8. va_end ( args );
  9.  
  10. return rv;
  11. }
  12.  
  13. int vprintf ( const char *fmt, va_list args )
  14. {
  15. return _print_stream ( stdout, fmt, args );
  16. }
vprintf is doing all of the work while printf is deferring to vprintf.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC