Search code examples
ccygwin

Invalid type argument of '*' (have 'double') C


I'm trying to learn C, and in my recent code came across a compile error that I do not understand. I don't really understand what the error means, so I therefor have trouble fixing the problem. I've done extensive googling, but didn't understand the explanations that i came across.

Can someone clarify?

The error:

cygwin compilartion output

The code:

#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);

Solution

  • You are dereferencing the wrong variable.

    If you look at your function watered then amount is of the type double*, i.e. a pointer to a double. However, watercredit is a global variable of type double. You cannot use the * operator on a double since that is not a pointer.

    This function should work:

    //takes the amount of water added and increases credit
    void watered(double* amount)
    {
        watercredit = watercredit + *amount;
    }