Solved

variable argument list

Posted on 2004-09-14
7
379 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ dll will not run on machine without VS installed 10 89
how to convert c++ code to Android App 3 110
Create a path if not exists 7 92
Indy 10 not Receiving UDP broadcast 3 23
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

821 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