Home > Back-end >  error: pointer value used where a floating point value was expected in C code
error: pointer value used where a floating point value was expected in C code

Time:12-02

I'm trying to write some code, and there is one error I don't understand why I keep getting. I want to write to a file, and I have some functions to return different information about the struct I have built. Here is my code:

IkResult productWriteToFile(AmountSet inventory, FILE *file){
    if (inventory == NULL) {
        return NULL_ARGUMENT;
    }

    fprintf(file, "Inventory status:\n");

    AS_FOREACH(Product, item, inventory){
        for(Product prod = (Product) asGetFirst(inventory); prod != NULL;
            prod = (Product) asGetNext(inventory)) {
            fprintf(file,"name: %s, id: %d, amount: %.3f, price: %.3f\n", getProductName(prod),
                    (int)getProductId(prod), prod -> amount, (double)((prod -> item) -> prodPrice));
        }
    }


    fclose(file);

    return SUCCESS;
}

and these are the "helper" functions:

unsigned int getProductId(Product prod){
    return (prod -> item) -> id;
}

char* getProductName(Product prod){
    return (prod -> item) -> name;
}

These is the error I am getting:

In function ‘productWriteToFile’:
item.c:183:21: error: pointer value used where a floating point value was expected
                     (int)getProductId(prod), prod -> amount, (double)((prod -> item) -> prodPrice));

Anybody knows what's the problem? Please help ><

UPDATE--- the structures are:

typedef double (*GetProductPrice)(ProductData, const double amount);
typedef void *ProductData;

struct product_t{
    struct item_t item;
    double amount;
    Product* next;
};

struct item_t{
    char* name;
    int id;
    GetProductPrice prodPrice;
    AmountType type;
    ProductData ProductData;
    CopyData copy;
    FreeData free_data;
};

CodePudding user response:

The prodPrice member has type GetProductPrice, whose type is as follows:

typedef double (*GetProductPrice)(ProductData, const double amount);

This is type is not double, but a pointer to a function that returns a double. So you need to call the function.

prod->item->prodPrice(prod->item->ProductData, prod->amount)
  • Related