[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

va_arg return value

Posted on 2000-01-12
10
Medium Priority
?
733 Views
Last Modified: 2012-05-04
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
Comment
Question by:olegsp
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 85

Expert Comment

by:ozo
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

by:obg
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

by:ozo
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 2

Expert Comment

by:abdij
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

by:
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

by:obg
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

by:abdij
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

by:obg
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

by:olegsp
ID: 2349378
Thanks to everyone for helpful comments.
0
 
LVL 2

Expert Comment

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

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

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

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question