2

I´m learning C now and tried to write my own function with a variable number of parameters, which I want to work with. Is this possible? I found examples how to create such a function but I don't know how to work with them. Here is what I have:

double ftest(int amount, double dA1,...)
{
    double dsumm = 0.0;
    for (int i=1;i <= amount; i++){
    dsum=1/dA1+1/dA2 //?? here is my Question how can I add all the parameters the user entered?
    }
    return dRges;
}

okay in my original post it was considered a duplicate, but I want to do more than just make a sum. I want to do different calculations with it. Like I want to be able to have them all as a them of dA1 to dAn = number of parameters. And then I want to do calculations.

Phoenix
  • 83
  • 2
  • 8
  • http://man7.org/linux/man-pages/man3/stdarg.3.html – melpomene May 13 '17 at 08:59
  • 2
    @MikeNakis I don't think it's a duplicate of that question (which has no good answer anyway). – melpomene May 13 '17 at 09:01
  • Before you ask a question on stackoverflow you are supposed to do at least a tiny little bit of research. At the very worst, look at the suggestions offered by stackoverflow ***as you are typing your question*** for previous questions that might already be about the same thing and already have answers. – Mike Nakis May 13 '17 at 09:03
  • 1
    It took a surprising number of pages of search results to find it, but the chosen duplicate basically asks for an explanation of a function which is very similar to what has been provided in the answers here — the difference is between `double function(int number, ...)` and `double function(int number, double arg1, ...)`. – Jonathan Leffler May 13 '17 at 09:17
  • 1
    Your edit changes the question quite a bit. Since there are already answers present, please don't make such drastic changes. In this case it would be better to ask a new question entirely and put emphasis on the fact that you wish to access the individual items of the va list. (It is a good question btw) – Lundin May 15 '17 at 11:25

2 Answers2

3

Try this (comments inline):

#include <stdio.h>
#include <stdarg.h> //This includes all the definitions for variable argument lists.

double add_nums(int amount, ...) 
{
    double total  = 0.0;
    va_list args; //This is working space for the unpacking process. Don't access it directly.
    va_start(args, amount); //This says you're going to start unpacking the arguments following amount.
    for (int i = 0; i < amount ; ++i) {
        double curr=va_arg(args, double);//This extracts the next argument as a double.
        total += curr;
    }
    va_end(args); //This says you've finished and any behind the scenes clean-up can take place.
    //Miss that line out and you might get bizarre behaviour and program crashes.
    return total;
}

int main() 
{
    double total=add_nums(4, 25.7, 25.7, 50.0, 50.0);
    printf("total==%f\n",total);
    return 0;
}

Expected output:

total==151.400000

There's a trap here because this is invalid:

double total=add_nums(4, 25.7, 25.7, 50, 50.0);

The fourth argument (50) is an integer. You must make sure you put a decimal in literals to make sure they're a bona fide double.

Persixty
  • 8,165
  • 2
  • 13
  • 35
  • I don't know if you want to do anything about it, but you have a marginally different signature, and you don't enforce that there is at least one argument to sum, and you don't enforce in the signature the type of that argument. That doesn't stop this being a decent answer; it is just nitpicking. – Jonathan Leffler May 13 '17 at 09:19
  • @JonathanLeffler I've not read that as the intent of the question. The code doesn't seem to be trying to use that first argument and they aren't doing something like calculating an average. I think we can all accept that the sum of no numbers is zero. – Persixty May 13 '17 at 09:24
2

Yes, it is possible and straight-forward:

#include <stdarg.h>

double ftest(int number, double dA1, ...)
{
    va_list args;
    va_start(args, dA1);
    double sum = dA1;
    for (int i = 1; i < number; i++)
        sum += va_arg(args, double);
    va_end(args);
    return sum;
}

And use in a function somewhere:

double d1 = ftest(2, 1.1, 2.3);
double d2 = ftest(1, 34.56);
double d3 = ftest(5, 21.23, 31.45, 9876.12, -12.3456, -199.21);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you for that, but I edited my first Question. It was just an example with the sum but I want to do more kind of calculations. Maybe you can look into that and see if it is possible as well? – Phoenix May 13 '17 at 09:44
  • 1
    @Phoenix One of the other answers linked takes an average. The key elements are `va_start`, `va_arg` and `va_end` with those you've got the tools to perform literally any definable calculation (in a formal sense). – Persixty May 13 '17 at 09:58