C错误,双精度要求双精度*
刚开始使用C,我的代码有一个错误:C错误,双精度要求双精度*,c,C,刚开始使用C,我的代码有一个错误: #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("%l
#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;
}
#包括
#包括
int main()
{
//声明变量
双速=1.4小时,总码数;
//提示用户输入小时数
printf(“输入小时数:”);
扫描频率(“%lf”、&hours);
//计算所占用的码数
总码=速度*小时;
//向用户显示结果
printf(“总码数为%.2f”,&totalYards);
返回0;
}
错误是
警告:格式“%f”要求参数类型为“double”,但参数2的类型为“double*”[-Wformat]|
在最后一次打印时更改
printf("The total amount of yards is %.2f", &totalYards);
到
您正在传递指针对象(类型double*
),但f
要求您传递类型double
的值
printf("The total amount of yards is %.2f", &totalYards);
到
您正在传递指针对象(类型
double*
),但f
要求您传递类型double
的值&totalYards
不是双精度。-这是指向双精度的地址(例如指针)。我觉得这几乎肯定是重复的,但我不知道有什么标准答案…&totalYards
不是双精度。-这是指向一个double的地址(例如指针)。我觉得这几乎肯定是重复的,但我不知道一个规范的答案……Add'l:重要的是要理解scanf
和printf
在这里是不同的printf
不接受指针,因为它只需要打印值scanf
确实接受指针,因为这是它获得对调用方堆栈框架中变量的写访问权的方式。(对于char
(通常被称为字符串)的数组,情况似乎并非如此,这当然会混淆问题。)(当然,在生产质量软件中,任何东西都不应该使用scanf
,但探究这种差异以及为什么必须存在这种差异对于获得语言能力都是有价值的。)@用户3561624,“谢谢”评论是不受欢迎的。你本可以接受答案的。补充:重要的是要理解scanf
和printf
在这里是不同的printf
不接受指针,因为它只需要打印值scanf
确实接受指针,因为这是它获得对调用方堆栈框架中变量的写访问权的方式。(对于char
(通常被称为字符串)的数组,情况似乎并非如此,这当然会混淆问题。)(当然,在生产质量软件中,任何东西都不应该使用scanf
,但探究这种差异以及为什么必须存在这种差异对于获得语言能力都是有价值的。)@用户3561624,“谢谢”评论是不受欢迎的。相反,你本可以接受这个答案。