variable number of arguments

Posted on 1998-10-14
Medium Priority
Last Modified: 2010-04-15
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.
Question by:940961sl
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
  • 2
LVL 86

Accepted Solution

jkr earned 300 total points
ID: 1253507
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...

Expert Comment

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


Author Comment

ID: 1253509
jkr, can you give me a little example of your solution number 3?
(I know, it's C here, but you started ... :-)
LVL 86

Expert Comment

ID: 1253510
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);


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

765 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