Link to home
Start Free TrialLog in
Avatar of lost_bits1110
lost_bits1110

asked on

variable argument list

Hello

I want my function to take in a varialbe number of arguments,
so for example if i had just one argument  I would call it by
function(1, arg1)

or if I had 2 arguments then I'd call
function(2, arg1, arg2)

or 3 then
function(3, arg1, arg2, arg3)
etc...

so I would have to have something like a switch statement to handle these variable cases

but what if I could possibly have 200 arguments
then how would I handle this?

i.e., how would I have a variable arugment list that doesnt require knowing the number of arguments in case the number of arguments could be from 1 to a million!

thanks..
Avatar of lost_bits1110
lost_bits1110

ASKER

or if someone could let me know if its possible at all...
Thanks
Avatar of jkr
There are a few ways using 'va_arg', e.g.

/* VA.C: The program below illustrates passing a variable
 * number of arguments using the following macros:
 *      va_start            va_arg              va_end
 *      va_list             va_dcl (UNIX only)
 */

#include <stdio.h>
#define ANSI            /* Comment out for UNIX version     */
#ifdef ANSI             /* ANSI compatible version          */
#include <stdarg.h>
int average( int first, ... );
#else                   /* UNIX compatible version          */
#include <varargs.h>
int average( va_list );
#endif

void main( void )
{
   /* Call with 3 integers (-1 is used as terminator). */
   printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );

   /* Call with 4 integers. */
   printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );

   /* Call with just -1 terminator. */
   printf( "Average is: %d\n", average( -1 ) );
}

/* Returns the average of a variable list of integers. */
#ifdef ANSI             /* ANSI compatible version    */
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 );
}
#else       /* UNIX compatible version must use old-style definition.  */
int average( va_alist )
va_dcl
{
   int i, count, sum;
   va_list marker;

   va_start( marker );            /* Initialize variable arguments. */
   for( sum = count = 0; (i = va_arg( marker, int)) != -1; count++ )
      sum += i;
   va_end( marker );              /* Reset variable arguments.      */
   return( sum ? (sum / count) : 0 );
}
#endif
Okay, but how in my code could I make your following statement

printf( "Average is: %d\n", average( 2, 3, 4, -1 ) )

more general?

because what if I have say 200 arguments then I would have to do

if (numArgs == 1)
printf( "Average is: %d\n", average( 1,1) );
if (numArgs == 2)
printf( "Average is: %d\n", average( 2, 1, 2 ) );
....etc....
if (numArgs == 200)
printf( "Average is: %d\n", average( 200, 3, 4, -1, etc.............. ) );

You mean you want to arrange/create the arguments at runtime?
yes precisely......
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
okay, even if the arguments i'm passing are arrays?