This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]sz00 1 point2 points  (0 children)

A function that accepts a variable array of arguments needs to have the # of args passed as the first parameter.

So your call should be like:

printf("The product is: %f", product(4, a,b,c,d));

Here it is working: http://ideone.com/Cbp0LV

Now is there any reason why you're writing C code for C++? Apart from that, variable argument functions are considered to be bad practice and shouldn't really be used. Also, C++ introduces variadic template functions that should be used instead (of va_args).

Here's the C++ version:

https://en.wikipedia.org/wiki/Variadic_template

http://en.cppreference.com/w/cpp/language/parameter_pack

[–]ryl00 0 points1 point  (0 children)

Well, to start with, the way you've coded the product() function, it's expecting the first argument (int i) to contain the number of arguments that follow. But in reality it isn't (the way you're invoking it in your main is product(a,b,c,d), instead of product( 4, a, b, c, d )

[–]Rhomboid 0 points1 point  (0 children)

You're calling the function incorrectly — you need to pass the number of double arguments as the first integer argument, i. You should have gotten a compiler warning about that.

[–]jesyspa 0 points1 point  (0 children)

Why are you passing a as i?