, , .
:
struct customer {
char name[20];
char surname[20];
int code;
float money;
};
-, float .
C FAQ 4.8. , , . ? -, , , , .. , , , .
, , -, , malloc. -, scanf . -, inserts, , .
, - create_customer_interactive, , , .
:
#include <stdio.h>
#include <stdlib.h>
struct customer {
char *name;
char *surname;
int code;
int money;
};
typedef struct customer *PCustomer;
int read_customer_record(FILE *fp, PCustomer customer) {
customer->name = "A. Tester";
customer->surname = "McDonald";
customer->code = 123;
customer->money = 100 * 100;
return 1;
}
PCustomer insert_record_interactive(PCustomer *db, FILE *fp) {
PCustomer tmp = malloc(sizeof(*tmp));
if (!tmp) {
return NULL;
}
if (!read_customer_record(fp, tmp)) {
free(tmp);
return NULL;
}
*db = tmp;
return tmp;
}
int main(void) {
PCustomer new_customer;
PCustomer *db = malloc(3 * sizeof(*db));
if (!db) {
perror("Failed to allocate room for customer records");
exit(EXIT_FAILURE);
}
new_customer = insert_record_interactive(&db[1], stdin);
if (!new_customer) {
perror("Failed to read customer record");
exit(EXIT_FAILURE);
}
printf(
"%s %s (%d) : $%.2f\n",
new_customer->name,
new_customer->surname,
new_customer->code,
((double)new_customer->money) / 100.0
);
return 0;
}