简体   繁体   English

函数未使用C中的指针返回正确的值

[英]Function doesn't return correct value using pointers in C

This is a program to calculate the prize for all friends who contributed for a bet. 这是一个计算为所有下注的朋友计算奖金的程序。 The reward is based on how much each one contributed with money. 奖励基于每个人用钱捐款多少。 But the function i created is returning 0.0000 for all. 但是我创建的函数将全部返回0.0000。

a little dictionary: 一些字典:

  • premio = prize 奖金=奖金
  • aposta = contribution of each friend aposta =每个朋友的贡献

int main(){
    int exerc;

    printf("Digite o numero do exercicio desejado: ");
    printf("\n");
    scanf("%d", &exerc);

    switch (exerc){
        case 2:{
            float aposta1, aposta2, aposta3, premio;

            printf("Digite respectivamente o valor que cada um apostou: ");
            scanf("%d %d %d", &aposta1, &aposta2, &aposta3);
            printf("Digite o valor do premio: ");
            scanf("%d", &premio);

            void exercicio2(float *, float*, float*, const float*);
            exercicio2(&aposta1, &aposta2, &aposta3, &premio);

            printf("O valor que o primeiro apostante recebera e de: %f", aposta1);
            printf("\nO valor que o segundo apostante recebera e de: %f", aposta2);
            printf("\nO valor que o terceiro apostante recebera e de: %f", aposta3);

        }
    }
    return 0;
}

void exercicio2(float *ap1, float *ap2, float *ap3, const float *premio){
    float total;

    total = *ap1 + *ap2 + *ap3;

    *ap1 = (*ap1/total) * (*premio);
    *ap2 = (*ap2/total) * (*premio);
    *ap3 = (*ap3/total) * (*premio);
}

An expected value for input (bets) aposta1 = 50, aposta2 = 25, aposta3 = 25 and premio(prize) = 1000, would be: ap1 = 500, ap2 = 250, ap3 = 250. 输入(下注)aposta1 = 50,aposta2 = 25,aposta3 = 25和premio(prize)= 1000的期望值将是:ap1 = 500,ap2 = 250,ap3 = 250。

but it's occurring: 但它正在发生:

"C:\Users\Marco Antonio\CLionProjects\untitled\cmake-build-debug\untitled.exe"
Digite o numero do exercicio desejado: 2
Digite respectivamente o valor que cada um apostou:50 25 25
Digite o valor do premio:1000
O valor que o primeiro apostante recebera e de: 0.000000
O valor que o segundo apostante recebera e de: 0.000000
O valor que o terceiro apostante recebera e de: 0.000000

Changing the format specifier ( %d to %f ) will solve the issue: 更改格式说明符( %d%f )将解决此问题:

scanf("%d %d %d", &aposta1, &aposta2, &aposta3);
scanf("%d", &premio);

to

scanf("%f %f %f", &aposta1, &aposta2, &aposta3);
scanf("%f", &premio);

The issue was you are entering a floating point number and your program is reading the integer. 问题是您输入的是浮点数,而程序正在读取整数。 In such case, the behaviour is undefined. 在这种情况下,行为是不确定的。 Please take a look at a SO question and it's answer about format specifier and undefined behaviour. 请看一个SO问题,它是有关格式说明符和未定义行为的答案

Standard specifies: 标准规定:

If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined. 如果任何参数都不是相应转换规范的正确类型,则行为未定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM