Programmantically finding the end in va_list
-
Hello, if you ware working with a variable parameter list like (int Number,...) how can you programmatically find out the number of parameters specified? MSDN Help for va_arg, va_end, va_start does it in the following way using -1 as an extra not processed last parameter. int average( int first, ... ) { int count = 0, sum = 0, i = first; va_list marker; va_start( marker, first ); /* Initialize variable arguments. */ while( i != -1 ) { sum += i; count++; i = va_arg( marker, int); } va_end( marker ); /* Reset variable arguments. */ return( sum ? (sum / count) : 0 ); } Are there better ways without a finishing last dummy parameter? :confused: --- Rainer Mangold
-
Hello, if you ware working with a variable parameter list like (int Number,...) how can you programmatically find out the number of parameters specified? MSDN Help for va_arg, va_end, va_start does it in the following way using -1 as an extra not processed last parameter. int average( int first, ... ) { int count = 0, sum = 0, i = first; va_list marker; va_start( marker, first ); /* Initialize variable arguments. */ while( i != -1 ) { sum += i; count++; i = va_arg( marker, int); } va_end( marker ); /* Reset variable arguments. */ return( sum ? (sum / count) : 0 ); } Are there better ways without a finishing last dummy parameter? :confused: --- Rainer Mangold
The other way is to pass # of arguments as one of 'normal' parameters. However, this increases the probability of an error - you could add one param and forget to increase passed # of param. Generally, you have to know what called function expects on the stack - there's no way to detect this 'automatically'. Tomasz Sowinski -- http://www.shooltz.com.pl
-
The other way is to pass # of arguments as one of 'normal' parameters. However, this increases the probability of an error - you could add one param and forget to increase passed # of param. Generally, you have to know what called function expects on the stack - there's no way to detect this 'automatically'. Tomasz Sowinski -- http://www.shooltz.com.pl
Thanks a lot, this was the answer I expected, although I hoped that there would be a nicer solution. -- Rainer