c - '*' 的类型参数无效(有 'double' )C

标签 c cygwin

我正在尝试学习 C,在我最近的代码中遇到了一个我不明白的编译错误。我不太明白该错误的含义,因此我无法解决该问题。我进行了大量的谷歌搜索,但不明白我遇到的解释。

谁能解释一下吗?

错误:

cygwin compilartion output

代码:

#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/

相关文章:

Python 2.7 未在 Cygwin 中加载

c++ - 检测后如何切割人脸

windows - Opa:找不到 Nodejs

c - 如何在 Cygwin 中执行文件?

linux - 如何删除文件的扩展名?

java - Win 7下无法在Cygwin中启动Marathon测试

c - 我正在尝试将一个头文件添加到另一个头文件中

c - 用C制作我自己的 turtle 图形,无法更改数组中元素的值

c - 你如何在 C 中将 void 指针转换为 char 指针

c - 将节点插入链表期间出现段错误