Problem with type in function with variable parameters in C

0

I'm having trouble solving a problem in a function that receives variable parameters. I have a function that adds a variable number of numbers, its first parameter is the number of numbers to be added, 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 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, if 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? Should not cast occurred when I pass variables of type int and specific that I want double variables? I noticed a certain difficulty when working with variable parameters in relation to type, in another example, if I try to add float parameters:

#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 problem running.:

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

    
asked by anonymous 18.09.2016 / 19:36

3 answers

0

You have to cast from int to double .

With this prototype double somaVariaveis(int qtd, ...) this function accepts parameters of any type in place of the 3 dots. In general case, it is not possible for the compiler to know how the parameters will be interpreted by the called function.

As for the second program, the message is very explanatory: when you pass parameters float to a function, the compiler "prompts" float to double when the function is called. However, your function considers float parameters, and since the sizes of float and double are different, your program will probably abort because the stack will become corrupted.

The two cases are similar.

This should also happen if your function with va_args treat char , and you pass char on the call, because in function call (if I'm not mistaken) char parameters are promoted to int .

    
18.09.2016 / 19:44
0

Face with explicit cast funf.

#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, (double) 10, (double) 20, (double) 30));

    return 0;
}
    
22.09.2016 / 02:53
0

If you only work with numerical values, casting works well with array.

#include <stdio.h>

double soma(int qtd, double s[]){
    double ret=0;
    for(int i=0;i<qtd;i++){
        ret+=s[i];
    }
    return ret;
}

int main()
{   
    int arg1=8;
    float arg2=4.3,arg3=5;
    double arg4=2.2,arg5=4;
    double n[100]={arg1,arg2,arg3,arg4,arg5,1,16.4};
    printf("\nsoma de parametros variaveis: %.2lf\n\n", soma(7, n));
    n[7]=200.0;
    printf("\nsoma de parametros variaveis: %.2lf\n\n", soma(8, n));
    return 0;
}
    
22.09.2016 / 10:23