[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# va_arg return value

Posted on 2000-01-12
Medium Priority
733 Views
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 ?
0
Question by:olegsp
• 4
• 3
• 2
• +1

LVL 85

Expert Comment

ID: 2348009
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

LVL 2

Expert Comment

ID: 2348738
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

LVL 85

Expert Comment

ID: 2348783
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

LVL 2

Expert Comment

ID: 2348801
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

LVL 2

Accepted Solution

abdij earned 200 total points
ID: 2348802
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

LVL 2

Expert Comment

ID: 2348913
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

LVL 2

Expert Comment

ID: 2348989
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

LVL 2

Expert Comment

ID: 2349057
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

LVL 1

Author Comment

ID: 2349378
0

LVL 2

Expert Comment

ID: 2349434
And you accepted that answer!? He didn't say anything that had not been said earlier... :-(
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
###### Suggested Courses
Course of the Month8 days, 19 hours left to enroll