Reputation: 43873
If each invocation of va_arg modifies the object declared with va_list so that the object points to the next argument in the list, is there any way to step back so that it points to the previous one, jump back to the first one, jump to the end? Go three quarters of the way though the list and then... you get the idea. Or is it a once passed it's gone type thing?
Upvotes: 1
Views: 380
Reputation: 1123
What are you doing that requires variable length arguments? I personally find them beyond useless. Being limited to a few basic data types and also the way you use/access them.
Instead it would probably be easier and safer to just pass a structure/class that contains the data you need.
Upvotes: 0
Reputation: 1977
You just need to be inventive. Variable argument list structure is platform dependent, however the elements are always stored in an array. On most x86 platforms va_lists are arrays of 64-bit blocks. That means that you can take a pointer to the first element in a va_list, cast it to a quad-word pointer and step through the list in any way you choose.
I used that strategy once for dissecting a va_list that we were passing into a middleware library that required all string data to be unicode formatted. Essentially I took a pointer to the first element of the va_list, stepped through each element until I found a string, and converted the string. Then I passed the va_list into the library.
Upvotes: 2
Reputation: 126418
With C99, you can save the state of a va_list with va_copy, allowing you to step through (part of) the list more than once.
Upvotes: 5
Reputation: 57784
You can "jump back" to the beginning by executing va_start
again. There is no other supported operation other than going on to the next argument.
However, most implementations use trivial pointer arithmetic. If you guarantee the code runs only on a particular architecture, then you can do the reverse of the arithmetic. But this completely defeats the purpose of va_list
which is to safely abstract the operations for all architectures.
Upvotes: 4