C错误,双倍预期加倍*

时间:2014-10-23 16:37:39

标签: c

刚开始使用C而我在代码中遇到1个错误:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    //declare variables
    double speed = 1.4, hours, totalYards;

    //prompt user to enter amount of hours
    printf("Enter the amount of hours: ");
    scanf("%lf", &hours);

    //calculate amount of yards taken
    totalYards = speed * hours;

    //display result to user
    printf("The total amount of yards is %.2f", &totalYards);

    return 0;
}

,错误是

警告:格式&#39;%f&#39;期望类型&#39; double&#39;的参数,但参数2的类型为&#39; double *&#39; [-Wformat] |

在最后一个printf。

1 个答案:

答案 0 :(得分:4)

更改

 printf("The total amount of yards is %.2f", &totalYards);

  printf("The total amount of yards is %.2f", totalYards);

您传递的是指针对象(类型为double *),但f要求您传递double类型的值。