3

EDIT: I am afraid I have not been clear.

What I wanted is to pass a (portion or all) of a vararg list to a sub-function without having to pass all the vararg parameters.

I did some research (trial & error) and found the solution, which I post as an answer.

I have deleted the original question because it was unclear. Thanks for your comments, which helped me to find the errors in my thoughts.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Why can't you simply pass `argv[2]` as a pointer to sum? You'd have to use `va_start`, `va_arg` etc. otherwise which is more of a hassle. – Linus Nov 02 '15 at 00:35
  • 1
    I think it's not even possible to pass them as varargs. Just pass the pointer. – Karoly Horvath Nov 02 '15 at 00:37
  • It's not standarized C atleast, it would require some assembly to manually push the desired parameters, and invoke the variadic function correctly. See [this](http://stackoverflow.com/questions/1721655/passing-parameters-dynamically-to-variadic-functions/1721684#1721684) answer for details. – Linus Nov 02 '15 at 00:38
  • You don't have variadic input to `main`, so how would you pass them as variadic? C is not Python where you can pack/unpack lists as arguments. You seem to be confusing an array of arbitrary size (open array) and variadic arguments. – too honest for this site Nov 02 '15 at 01:05
  • @Linus: This would indeed be very implementation-specific and requires to study the PCS/ABI of the target platform. However, this is nonsense here, as the arguments are already in an array. – too honest for this site Nov 02 '15 at 01:07
  • Why do you thing `argv` is located on the stack? There is no requirement (and no proof). Indeed, C does not even require a stack. – too honest for this site Nov 02 '15 at 01:08
  • I don't know what you mean by the last paragraph, can you rephrase it or show a code example of what you mean? – M.M Nov 02 '15 at 02:46
  • @m-m (and others), I rephrased my question and with trial & error came up with a solution. It was more simple than I thought. – Paul Ogilvie Nov 02 '15 at 12:30

1 Answers1

0

The following is a main test function. It calls a function that can take a variable number of arguments. That funtion in turn calls a sub-function to do the work. It gets passed the argument list, but not by passing the parameters but by passing the address of the vararg list. It then processes this list:

void va_test1(int n,...);
void va_test2(int n, va_list argptr);

int va_main(void)
{
    va_test1(5, 1,2,3,4,5);
    return(1);
}
void va_test1(int n,...)
{
    va_list argptr;

    va_start (argptr, n);
    va_test2 (n, argptr);
    va_end (argptr);
}
void va_test2(int n, va_list argptr)
{
    int i;
    for (i=0;i<n;i++)
        printf("%d\n", va_arg( argptr, int ));
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Your `va_test2`'s second parameter should just be `va_list argptr`. It is not guaranteed that you can assign `void *` to `va_list` or vice versa. Also, you should `va_end` when you're done. – newacct Nov 04 '15 at 01:17
  • @newacct, thanks for your comments. I have updated the solution. `va_end` seems not necessary as it only resets the `argptr`. – Paul Ogilvie Nov 04 '15 at 13:44