Solved

Reinventing the wheel...

Posted on 1998-09-16
16
282 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:Haho
  • 6
  • 3
  • 2
  • +5
16 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1252808
#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
 
LVL 1

Author Comment

by:Haho
ID: 1252809
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
 
LVL 3

Expert Comment

by:braveheart
ID: 1252810
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
 
LVL 84

Expert Comment

by:ozo
ID: 1252811
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
 
LVL 3

Expert Comment

by:xyu
ID: 1252812
:)
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
 
LVL 1

Author Comment

by:Haho
ID: 1252813
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
 
LVL 1

Expert Comment

by:Bonev
ID: 1252814
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
 
LVL 1

Author Comment

by:Haho
ID: 1252815
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:Bonev
ID: 1252816
No, I mean something like myfunc("%d%d%f", 1, 2, 3.5);
0
 
LVL 1

Author Comment

by:Haho
ID: 1252817
thanks!!!
0
 

Expert Comment

by:pbk
ID: 1252818
haho, check the documentation on varg and vstring vsprintf and all those starting with v!!

that's the answer to your question!!!
0
 

Expert Comment

by:AnthonyJ
ID: 1252819
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
 
LVL 1

Author Comment

by:Haho
ID: 1252820
thanks for your comment
0
 
LVL 1

Expert Comment

by:Bonev
ID: 1252821
AnthonyJ,
Thanks

Haho,
What do you think about my idea? is it ok for you or you are looking for another solution?
0
 

Accepted Solution

by:
hawkett092198 earned 150 total points
ID: 1252822
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
 
LVL 1

Author Comment

by:Haho
ID: 1252823
Bonev,
  it is a good suggestion .. thanks!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

746 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

11 Experts available now in Live!

Get 1:1 Help Now