Solved

variable argument list

Posted on 2004-09-14
7
378 Views
Last Modified: 2010-04-01
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..
0
Comment
Question by:lost_bits1110
  • 4
  • 3
7 Comments
 

Author Comment

by:lost_bits1110
ID: 12057383
or if someone could let me know if its possible at all...
Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 12057413
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
0
 

Author Comment

by:lost_bits1110
ID: 12057565
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.............. ) );

0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 86

Expert Comment

by:jkr
ID: 12057587
You mean you want to arrange/create the arguments at runtime?
0
 

Author Comment

by:lost_bits1110
ID: 12057632
yes precisely......
0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 12057689
Well, then, a variable list of arguments is not what you want - you'd pass an array of arguments instead, e.g.

int average ( int a[], int count) {

    int sum = 0;

    for ( int i = 0; i < count; ++i) sum += a[i];

    return sum / count;
}

int an [] = { 1,2,3,4,5,6,7,8,9};

int avg;

avg = average ( an, 4);


avg = average ( an, 9);

0
 

Author Comment

by:lost_bits1110
ID: 12058450
okay, even if the arguments i'm passing are arrays?
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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