Solved

variable number of arguments

Posted on 1998-10-14
4
254 Views
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;
      va_end(argp);
      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.
}
0
Comment
Question by:940961sl
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
Comment Utility
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...
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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
Example:
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)
Example:
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,
e.g

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 ) ;
or
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.

0
 
LVL 1

Author Comment

by:940961sl
Comment Utility
jkr, can you give me a little example of your solution number 3?
(I know, it's C here, but you started ... :-)
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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);}
#else
#error Hey, this is plain C !!!
#endif

void main ( void)
{
 int i;

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

}
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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 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

10 Experts available now in Live!

Get 1:1 Help Now