我正在尝试学习 C,在我最近的代码中遇到了一个我不明白的编译错误。我不太明白该错误的含义,因此我无法解决该问题。我进行了大量的谷歌搜索,但不明白我遇到的解释。
谁能解释一下吗?
错误:
代码:
#include <stdio.h>
#include <unistd.h>
#include "plant.h"
double watercredit = 0.0;
int needwater = 200;
double wateredamount = 0.0;
int main()
{
watercredit=215.00;
while(watercredit > 0.0)
{
watercredit--;
if(watercredit < needwater)
{
printf("You need to water the plant!\n");
printf("enter amount of water:\n");
scanf("%lf", wateredamount);
watered(&wateredamount);
//watercredit = watercredit + wateredamount;
wateredamount = 0;
}
if(watercredit == 0)
{
printf("You plant died!");
return 0;
}
printf("Watercredit: %lf\n", watercredit);
sleep(1);
}
return 0;
}
//takes the amount of water added and increases credit
void watered(double* amount)
{
*watercredit = *watercredit + amount;
}
Plant.h:
void watered(double* amount);
最佳答案
您取消引用了错误的变量。
如果您查看您的函数,则 amount
的类型为 double*
,即指向 double 的指针。但是,watercredit 是 double 类型的全局变量。您不能在 double 上使用 *
运算符,因为它不是指针。
这个函数应该可以工作:
//takes the amount of water added and increases credit
void watered(double* amount)
{
watercredit = watercredit + *amount;
}
关于c - '*' 的类型参数无效(有 'double' )C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32376075/