Solved

variable argument list

Posted on 2004-09-14
7
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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: 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.

Question has a verified solution.

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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

623 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