Solved

variable argument list

Posted on 2004-09-14
7
374 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now