• C

variable number of arguments

I've got this function (ripped from the VC++ help :-) to add a variable number of integer together. Problem is : I don't like to add that last argument to close the list. Is there a way around this unprofessional looking thing.

#include <stdarg.h>

int add(int first, ...)
      va_list                  argp;
      int                  i, count = first;

      va_start(argp, first);
      while((i = va_arg(argp, int)) != 0)
            count += i;
      return count;

void main(void)
      add(19, 27, 16, 36, 5, 4, 0);
        // add(19, 27, 16, 36, 5, 4);  this is what I want.
Who is Participating?
Well, there are 3 ways:

1. Think of something like a 'printf()' format string (this would also allow to pass values of different types, such as intergers AND doubles/floats)

2. use the number of arguments as the first argument

3. switch to C++ and use overloaded functions (i.e. functions that differ only in the argument list

Sorry, but yo can't detect the number of arguments passes at runtime...
jkr types faster than me, so I'll submit as a comment:

>> // add(19, 27, 16, 36, 5, 4);  this is what I want.
You can't really do this as you need to tell va_start/va_end when to stop

There are several ways you can design your add function.

1. Use a sentinel value (like you do in your example) to mark the end of the list

2. Pass the number of arguments as the first param of add
add(6, 19, 27, 16, 36, 5, 4);  /* 6 means add 6 numbers */

3. Use a format specifier (like printf or scanf do) as the first argument (this is really an elaboriation of idea #2)
add("%d%d%d%d%d%d", 19, 27, 16, 36, 5, 4);  

The string specifiers the number and argument types.  The cool part with this idea is you can expand add to handle other types, for example doubles, floats, longs, shorts as well as just ints.

4. The other main alternative is to use an array as the input to the function,

tnt add( int values[], int valuecount ) ;

-- If you do some pre-processor you can avoid passing the count usually
#define ADD( _array_ )    add( _array_, sizeof(_array_) / sizeof(int) )

- and call
int x[3] = { 1, 2, 3 } ;
int total = ADD( x ) ;
int total = add( x, 3 ) ;

Downside for this is the macro ADD only works when arrays are still arrays (not if you have a ptr to an array, for example passed into a function).

If you look at the standard library it uses approaches 3 or 4 (without the macro idea, probably because of the restriction I mentioned) - so this is the model you should follow in designing your own function.

940961slAuthor Commented:
jkr, can you give me a little example of your solution number 3?
(I know, it's C here, but you started ... :-)
Of course ;-)

#ifdef __cplusplus // defined in VC5 when compiling .cpp
int add ( int nArg1, int nArg2)
{ return ( nArg1 + nArg2);}
int add ( int nArg1, int nArg2, int nArg3)
{ return ( nArg1 + nArg2 + nArg3);}
int add ( int nArg1, int nArg2, int nArg3, int nArg4)
{ return ( nArg1 + nArg2 + nArg3 + nArg4);}
#error Hey, this is plain C !!!

void main ( void)
 int i;

 i = add ( 1, 2);
 i = add ( 1, 2, 3);
 i = add ( 1, 2, 3, 4);

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.