23753

How can I get my va_list arguments to repeat itself?

Question:

This is my first time working with va_list and stuff so I don't really know what I'm doing. Okay basically what I have is a bunch of numbers (1, 2, 3, 4, 5) in the function ordered and I get them to print out. This works fine.

#include <iostream> #include <cstdarg> using namespace std; void ordered(int num1, double list ...); void main() { ordered(5, 1.0, 2.0, 3.0, 4.0, 5.0); } void ordered(int num1, double list ...) { va_list arguments; va_start(arguments, num1); list = va_arg(arguments, double); cout << "There are " << num1 << " numbers" << endl; do { cout << list << endl; // prints out 1 then 2 then 3 then 4 then 5 list = va_arg(arguments, double); } while (list != 0); // at this point, list = 0 va_end(arguments); }

the problem is, after the va_end(arguments); or before it, I would like to get the program to print out my list a second time; basically print out 1, 2, 3, 4, 5 once more, without making another function. I tried to duplicate the code:

va_start(arguments, num1); do { cout << list << endl; list = va_arg(arguments, double); } while (list != 0); va_end(arguments);

without success. How can the program to repeat list once more, or is it not possible to do it again in the same function?

Answer1:

Here is a working implementation:

#include <iostream> #include <cstdarg> using namespace std; void ordered(int num1, ...); // notice changed signature int main(int,char**) { ordered(5, 1.0, 2.0, 3.0, 4.0, 5.0); return 0; } void ordered(int count, ...) // notice changed signature { va_list arguments; va_start(arguments, count); cout << "There are " << count << " numbers" << endl; double value = 0.0; // notice how the loop changed for(int i = 0; i < count; ++i) { value = va_arg(arguments, double); cout << value << endl; // prints out 1 then 2 then 3 then 4 then 5 } // at this point, list = 0 va_end(arguments); va_list arg2; va_start(arg2, count); cout << "There are " << count << " numbers" << endl; for(int i = 0; i < count; ++i) { value = va_arg(arg2, double); cout << value << endl; // prints out 1 then 2 then 3 then 4 then 5 } // at this point, list = 0 va_end(arg2); }

Answer2:

From the man page:

<blockquote>

va_end()

Each invocation of va_start() must be matched by a corresponding invocation of va_end() in the same function. After the call va_end(ap) the variable ap is undefined.

Multiple traversals of the list, each bracketed by va_start() and va_end() are possible.

</blockquote>

Could you show the code where you <em>tried that but it didn't work</em>?

NB. See also va_copy, with which you can make a duplicate of arguments before (destructively) traversing it, and then also traverse the duplicate.

Answer3:

The simple answer (ignoring how varargs really works, I find it hard to find a valid use case outside of printf) is to copy the arguments yourself. Well, actually a simpler answer would be not to use varargs at all... Why are you not passing a container (or in C++11 using an initializer_list?)

Recommend

  • How can printf function can take variable parameters in number while output them?
  • Using sizeof with sprintf/vsnprintf corrupts text
  • Variadic list parameter
  • c - how to make a function with multiple arguments (va_list)?
  • How is vprintf implemented? [duplicate]
  • Using a function with variable argument strings
  • Custom Membership/Role/Profile Providers WITHOUT inheriting the MembershipProvider, RoleProvider, et
  • va_arg( , double) return each time an invalid value [closed]
  • use rvest and css selector to extract table from scraped search results
  • Which browser have this strange user agent? (IOS device)
  • How dotnet build chooses the output name
  • Varnish/Apache Random 503 Errors
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Unable to connect to OnVif enabled camera using C#
  • .NET video play library which allows to change the playback rate?
  • Cuda Clang and OS X Mavericks
  • Using Laravel 5.4 pusher
  • Getting different value with placeholder over CPU/GPU
  • hide missing dates from x-axis ggplot2
  • C# fibonacci function returning errors
  • How to get current document uri in XSLT?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • What is the purpose of TaskExecutor in spring?
  • Oledb connection string for excel files
  • Use of this Javascript
  • Linq Objects Group By & Sum
  • Illegal mix of collations for operation for date/time comparison
  • Counter field in MS Access, how to generate?
  • output of program is not same as passed argument
  • MySQL WHERE-condition in procedure ignored
  • Akka Routing: Reply's send to router ends up as dead letters
  • Arrays break string types in Julia
  • How to format a variable of double type
  • Buffer size for converting unsigned long to string
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Checking variable from a different class in C#
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?