0

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;
};
13
  • What type is prod->amount? Commented Dec 1, 2021 at 18:46
  • Hiding pointers behind typedefs makes the code hard to read, especially as the types are not shown. Commented Dec 1, 2021 at 18:48
  • @WeatherVane sorry!! Added here the structures! Commented Dec 1, 2021 at 18:52
  • 1
    ok, so what's the definition of GetProductPrice? Commented Dec 1, 2021 at 18:58
  • 1
    The (prod -> item) -> prodPrice suggests the item is a pointer, but it isn't, it's a struct. Commented Dec 1, 2021 at 18:58

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.