Re: sum(2,4,3,5,-1,...)



Allan Adler wrote:
I don't understand how to write functions that have variable numbers
of arguments. The only example I have to imitate is the one on p.289
of C: A Reference Manual, 2d ed (1987), by Harbison and Steele, for
a function named printargs, which is also my only source of information
about how to write such functions. I tried it on my PC running RH 7.1
and it compiles and runs fine. It depends, however, on an array at[]
for which &at[0] is the first argument. There is no natural choice
of an array related to the first argument of the function sum I'm
trying to write, but GCC complains that the first argument to sum
is not a pointer, so I think I have to give it one. Basically, I
wrote sum by imitating the code for printargs mentioned above.

#include <stdio.h>
#include <stdarg.h>

int sum(int *argtypep, ...)
{
va_list ap;
int argtype;
int total=0;
va_start(ap,argtypep);
while((argtype = *argtypep++) != 0) total += va_arg(ap,int);
va_end(ap);
return total;
}

The function sum is supposed to take a variable number of arguments
of type int and add them up. Thus, sum(0) returns 0 and sum(3,5,2,-7)
returns 3.

You cannot do this in C. In most implementations of C, e.g. using the
stack to push the arguments from right to left, the function with
ellipsis cannot determine what is the number of its arguments unless
there are some constraints on them.
One example: the number of arguments is the first arg:
int sum (int num_args, ...);
....
... sum(4, 3,5,2,-7) ... /* will return 3 */

Other example: you set a special number to end the sequence:
int sum(int first, ...); /* put -9999 at the end */
...
.... sum(3,5,2,-7,-999) ... /* will return -3 */

The problem with these solutions is that you cannot call sum(), with
no args, and get 0, like you can do in Scheme or C++.

In C++ you can use the default arguments for the 1st style, with the
number of args to sum:
int sum(int num_args=0, ...);

In this case your 'sum()' will return 0.

Michael
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.



Relevant Pages

  • Re: Performance of hand-optimised assembly
    ... Why not use unsigned for sum in the test harness? ... int is better yet because the output will then be same across machines. ... gcc -O3 seems ... but taking an average by eye my best asm version was *slower* than ...
    (comp.lang.c)
  • Re: [PATCH] input: Introduce light-weight contact tracking
    ... seems the patch was damaged somehow. ... static int illegal(int nslot, int npos, unsigned x) ... int i, j, sum; ...
    (Linux-Kernel)
  • Re: Algorithm
    ... Wont sum of all positive numbers will be the largest sub-array? ... int getint ... struct sofar *next; ... struct sofar *discard(struct sofar *trail) ...
    (comp.lang.c)
  • Re: Arrays vs Buffers
    ... and the next array access is dependent on the value ... and then return the overall sum to the test harness ... nextPointer(int[] array, int point) ... pointer = nextPointer; ...
    (comp.lang.java.programmer)
  • C program to look for large values of | zeta( 1/2 + it) | w.r.t. t
    ... the sum of the terms passed #20,000 in the RS formula. ... int i, j, k; ... unsigned long Nbill; ... int excess; ...
    (sci.math)