• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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

  • 6
  • 3
  • 2
  • +5
1 Solution
#include <stdarg.h>

type v_arg(va-list ap, type)
#define Add(a,b) ((a)+(b))
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 )

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.
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

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


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 )

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.

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)?

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

that's the answer to your question!!!
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)
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.
HahoAuthor Commented:
thanks for your comment

What do you think about my idea? is it ok for you or you are looking for another solution?
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,

HahoAuthor Commented:
  it is a good suggestion .. thanks!!
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.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 6
  • 3
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now