To convert text to a float
, use strtof()
. strtod()
is better for double
.
Your initialize routine likely wants a copy of the `date, etc.
A suggested improved routine follows:
expense_record initialize(const char * date, int quantity, const char *price, const char *item) {
expense_record e;
char *endptr;
e.date = strdup(date);
e.quantity = quantity;
e.item = strdup(item);
if (!e.date || !e.item) {
; // handle out-of -memory
}
e.price = strtof(price, &endptr);
if (*endptr) {
; // handle price syntax error
}
return e;
}
BTW: Recommend an additional change to pass into your initialization the destination record, but that gets into higher level architecture.