I want to make method take infinity arguments in objective-c and add these arguments .
-
1Pass a single argument of type `double`. Pass C99's `INFINITY` as the value. (attn: humorous comment) – Nikolai Ruhe Dec 02 '13 at 20:33
-
1@NikolaiRuhe `1.0 / 0.0` makes it C89-compatible :P – Dec 02 '13 at 20:34
-
1@H2CO3 That's depending on the architecture. C89 (IEEE 754) does not guarantee non-signalling division-by-zero. – Nikolai Ruhe Dec 02 '13 at 20:35
2 Answers
I'm assuming by "infinite" you mean "as many as I want, as long, as memory allows".
Pass a single argument of type NSArray
. Fill it with as many arguments as you wish.
If all arguments are guaranteed to be non-object datatypes - i. e. int, long, char, float, double, struct's and arrays of them - you might be better of with a NSData
. But identifying individual values in a binary blob will be trickier.
Since you want to add them up, I assume they're numbers. Are they all the same datatype? Then pass an array (and old style C array) or a pointer, and also a number of elements.
EDIT: now that I think of it, the whole design is fishy. You want a method that takes an arbitrarily large number of arguments and adds them up. But the typing effort required for passing them into a function is comparable to that of summing them up. If you have a varargs function, Sum(a,b,c,d,e)
takes less typing than a+b+c+d+e
. If you have a container class (NSArray, NSData, etc), you have to loop through the addends; while you're doing that, you might as well sum them up.

- 59,826
- 25
- 160
- 281
-
-
Not enough information for that. The NSData approach only makes sense if you have multiple *number* arguments of *different* numeric types. I don't know if that's the case. – Seva Alekseyev Dec 03 '13 at 20:59
That's not possible on a finite machine (that is, all existing computers).
If you're good with a variable, yet finite, amount of arguments, there are C's ...
variadic argument functions.

- 81,520
- 17
- 180
- 200