• C

va_arg return value

I am writing a function with variable number of arguments. So I found (MSDN) the following example:
int average( int first, ... )
{
   int count = 0, sum = 0, i = first;
   va_list marker;

   va_start( marker, first );    
   while( i != -1 )
   {
      sum += i;
      count++;
      i = va_arg( marker, int);
   }
   va_end( marker );              
   return( sum ? (sum / count) : 0 );
}

But what happens if one of the arguments ("first" or any other) is equal to -1 ? Then
while( i != -1 )
will not execute.  Moreover, I've seen another example where 0 is used instead of -1. Do I have to assume that my arguments cannot have certain values ?

What is the correct termination condition for va_arg iteration ?
LVL 1
olegspAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
Your function decides on the correct termination condition for va_arg iteration,
and the caller of the function should ensure that the arguments passed conform to those assumtions.
0
obgCommented:
An easy way to solve this is to send the number of arguments as the first parameter. I even think it's the only way if you do not want to have a terminator number.
0
ozoCommented:
There are many other possible ways.
The first argument could define a terminator number.
The first argument could be a string describing the following arguments.
Every other number could be a flag indicating whether any arguments follow.
The arguments could be in increasing order except for the last
There might be a pair of terminating values in the last two arguments.
The first argument could select which of the above methods to use
.....
0
Redefining Cyber Security w/ AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Join our webinar on Sept. 21st to learn more about leveraging AI and machine learning to protect your business.

abdijCommented:
Hi,
 For your function to work the Caller of the function average(...) should ensure that the last item is -1. This should be properly documented. Or if you find that -1 can probably be valid input (since the marker can contain a valid value as -1 ), then use a value that probably will not be encountered (for eg -32767). Rest seems fine. It is just an indication that the list has come to an end.

Obq: If he knew the number of arguments why would he use va_arg()?? He could have resolved in a simple loop.
Yet if he can yours is also a valid solution, but then va_arg() is useless

Bye
All the best
ABdij
0
abdijCommented:
Hi,
 For your function to work the Caller of the function average(...) should ensure that the last item is -1. This should be properly documented. Or if you find that -1 can probably be valid input (since the marker can contain a valid value as -1 ), then use a value that probably will not be encountered (for eg -32767). Rest seems fine. It is just an indication that the list has come to an end.

Obq: If he knew the number of arguments why would he use va_arg()?? He could have resolved in a simple loop.
Yet if he can yours is also a valid solution, but then va_arg() is useless

Bye
All the best
ABdij
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
obgCommented:
abdij: Of course the caller knows the number of arguments. The reason to use a va_list is so that different callers can use a different number of arguments. (Perhaps tha caller can set up dynamic va_lists as well. I have never tried that, and I don't think that is the indended purpose.) And, as I've understood, he wanted to avoid a terminator number.
0
abdijCommented:
Hi,
 Sorry obq. Then ofcourse as i said earlier your solution is valid too.
But does he want to avoid the terminator number ??
I think ozos solution is good enough.

Bye
Abdij
0
obgCommented:
He wrote "Do I have to assume that my arguments cannot have certain values ?" I say he does not have to assume that. I still think the best solution is to send the number of arguments as first parameter.

If more flexibility is required regarding types and stuff, then a format specifier of some kind would be required (like ozo mentioned). - Like printf/scanf for example.
0
olegspAuthor Commented:
Thanks to everyone for helpful comments.
0
obgCommented:
And you accepted that answer!? He didn't say anything that had not been said earlier... :-(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.