0
I’m having trouble solving a problem in a function that receives variable parameters. I have a function that sums a variable amount of numbers, its first parameter is the amount of numbers to be summed, and the others are the numbers themselves, it works when working with double numbers, but not when using numbers of type int, compiling this code the output will be 60.00, as expected:
#include <stdio.h>
#include <stdarg.h>
double somaVariaveis(int qtd, ...)
{
va_list args;
int i;
double soma = 0;
va_start(args, qtd);
for(i = 0; i < qtd; i++)
soma += va_arg(args, double);
va_end(args);
return soma;
}
int main()
{
printf("soma de parametros variaveis: %.2lf", somaVariaveis(3, 10.0, 20.0, 30.0));
return 0;
}
Now, in case I try to print the sum of integers:
printf("soma de parametros variaveis: %.2lf", somaVariaveis(3, 10, 20, 30));
The result will be 0.00;
Where did I go wrong? Shouldn’t I have cast when I pass variables of type int and specify that I want double variables? I noticed some difficulty when working with variable parameters in relation to the type, in another example, if I try to add parameters of type float:
#include <stdio.h>
#include <stdarg.h>
float somaVariaveis(int qtd, ...)
{
va_list args;
int i;
float soma = 0;
va_start(args, qtd);
for(i = 0; i < qtd; i++)
soma += va_arg(args, float);
va_end(args);
return soma;
}
int main()
{
printf("soma de parametros variaveis: %.2f", somaVariaveis(3, 10.0f, 20.0f, 30.0f));
return 0;
}
I get the following Warning, and I have a running problem.:
Warning: 'float' is promoted to 'double' when passed through '...'
note: (so you should pass 'double' not 'float' to 'va_arg')
note: if this code is reached, the program will abort
But how to cast in this case? because in va_arg, I already need to inform the type that I want to convert the parameter passed.
– Luis Henrique
If it is constant use 10. , 20. , 30. etc., or if you think it is better to use 10.0, 20.0, 30.0, etc. (i.e., place the decimal point). If it is a use variable (double)i in C, double(i) in C++...if it is a use expression (double)(a+b etc) in C, double(a+b etc) in C++. Actually in C++ there are more advisable cast forms, but I don’t use them.
– zentrunix
I get it, you’re talking about making an explicit cast at the moment of passing parameters. What I wanted was exactly a way to get through as an integer and still be able to convert it into double within the function.
– Luis Henrique
When you use the 3 dots I have no way, you have to pass the parameters of the type that the function waits for internally. Now, if you use C++ normally it is not necessary to use functions with variable parameter numbers, C++ has much more features than C, especially if you use C++11 or C++14.
– zentrunix