Problem regarding type in function with variable parameters in C

Asked

Viewed 336 times

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

3 answers

0

Face with explicit function cast.

#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;
}

0


You have to cast the int for double.

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

As for the second program, the message is very explanatory: when you pass parameters float for a function, the compiler "promotes" the float for double when the function is called. However, its function considers parameters float, and as the sizes of float and double are different, your program will probably abort as the execution stack (stack) will get corrupted.

The two cases are similar.

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

  • 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.

  • 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.

  • 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.

  • 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.

0

If working only 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.