• C

Reinventing the wheel...

Hi guys,
  I am trying to do a add function that does what operator + do, but in this form : result = Add(a,b,c.....);
<don't ask me why ;) >
My question is:
 1. How can I make it accept optional number of parameters?
    (minimum is 2, no maximum)
2. How can I make it accept any numerical type such as int, long, float, double?

Thanks a lot,guys.
If anybody has the unix source for the operator '+', pls. post it as well so I can see how it is actually implemented... :)
Any code examples would be super..

David
LVL 1
HahoAsked:
Who is Participating?
 
hawkett092198Connect With a Mentor Commented:
HaHo, Another solution (which may not be what you're after, but will give you unlimited number of parameters without pre-specifying), try creating a list structure and passing that, (or if thats sounds a bit slow) try creating a really big array, bigger than (or as big as) the most parameters you want, and pass that.  It seems sensible to put all your arguments in a contiguous piece of memory and just pass a pointer to the first one (which is exactly what the array solution will do) - the list structure will only be useful if you haven't got the memory for the big array.  Initialise your array to -1's, or put the number of arguments in the first array position so you know when to stop reading them.  With the list option, the last 'next' pointer will be NULL so you won't have to specify the number of arguments.  Good luck,

                              Colin
0
 
ozoCommented:
#include <stdarg.h>
va_start
va_arg
va_end


type v_arg(va-list ap, type)
   or
#define Add(a,b) ((a)+(b))
   or
C++
0
 
HahoAuthor Commented:
thanks ozo,
   I used <vararg.h> since I'm Unix-based and I am calling :
  my for loop-->
   for (.., i = va_arg(ap, int)) != -1; ... )  do something...
  and I call my function by :
   va_myfunc( .. ,..,.. ,.., -1 ) where -1 is the last indicator...
  BUT I don't want an indicator ... it looks unprofessional...
  I just want to call my myfunction this way: va_func( .., .. ,... ,... )
  Is there any way? (perhaps to get the count of params )
 

 
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
braveheartCommented:
You don't need the indicator but you do need to accompany each argument with its type, unless you are using C++ which does it for you. See ozo's suggestion.

What do you mean by posting source for the "+" operator? It is built into the compiler with every compiler being different. Each different architecture may have more than one compiler so there is no such thing as a single UNIX compiler, although perhaps the GNU C Compiler is the nearest thing. It is probably coded in assembler in most compilers.
0
 
ozoCommented:
Not portably (unless you have the first argument describe the args which follow)

If you want to be nasty and non-portable, comparing ap before and after a va_end might tell you something on particular implementations...
But I don't think I'd want to get anywhere near code that tried that.
0
 
xyuCommented:
:)
try this  :)
template <class T> T Add(
  T a1,
  T a2 = 0,
  T a3 = 0,
  T a4 = 0,
  T a5 = 0,
  T a6 = 0,
  T a7 = 0,
  T a8 = 0,
  T a9 = 0,
  T a10 = 0,
  T a11 = 0,
  T a12 = 0,
  T a13 = 0
){
  return (
    a1 + a2 + a3 + a4 + a5 +
    a6 + a7 + a8 + a9 + a10 +
    a11 + a12 + a13
  );
}

:)

0
 
HahoAuthor Commented:
I want to do it in C, not C++... :) Thanks anyway , xyu
furthermore your template example has fixed the number of params to add,
whereby I was looking for a variable params list, which I have done by using the varargs.h lib ( ozo's contribution )...

As for the operator source, pls ignore that part cause as braveheart has said, it probably is built in to the compiler / or written with assembly lang..  at first I tought it was built around C lang. ( like MFC )

0
 
BonevCommented:
I agree with ozo - do not try to guess what are the types of the operands.
To figure out this you can use the printf approach - pass the arguments' types as first parameter.


0
 
HahoAuthor Commented:
something like va_myfunc((int), 1,10, 3,...)???
Then based on the first parameter , I would check each param until it is not an int ( or whatver type)?


0
 
BonevCommented:
No, I mean something like myfunc("%d%d%f", 1, 2, 3.5);
0
 
HahoAuthor Commented:
thanks!!!
0
 
pbkCommented:
haho, check the documentation on varg and vstring vsprintf and all those starting with v!!

that's the answer to your question!!!
0
 
AnthonyJCommented:
Bonev is right. The only thing I can add is that you don't need the %. In printf() the % sign is used as a delimiter to signify something special happening in the output,
for example:

printf( "Tiddles %02d", 1)
   gives
Tiddles 01

In your function you won't need the %, since in your function, only the type of input parameter needs to be determined: the output is a returned value.

Removing the percent signs may also improve readability... then again it may not! What this does do is, reduce the parsing overhead in your function.
0
 
HahoAuthor Commented:
thanks for your comment
0
 
BonevCommented:
AnthonyJ,
Thanks

Haho,
What do you think about my idea? is it ok for you or you are looking for another solution?
0
 
HahoAuthor Commented:
Bonev,
  it is a good suggestion .. thanks!!
0
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.