I'm quite new to structs and I'm trying to make a webshop for an exercise. I have a struct which contains a product, an id, a price and a pointer to the next one.
struct product_t
{
char *product;
int id;
float price;
struct product_t *next;
};
I'm asked to make a create function as given below:
struct product_t *createProduct(char *data, int id)
so I thought of creating a new struct called data with pointers referring to the product struct:
struct data {
struct product_t *product;
struct product_t *price;
struct product_t *id;
struct data *next;
};
But when I use the create function like I did below I get errors:
struct product_t *createProduct(char *data, int id) {
struct data *New = malloc(sizeof(struct data));
New->product = (char*) malloc(sizeof(char)*sizeof(data)+1);
strcpy(New->product, data);
New->price = price;
New->id = id;
return New;
}
Edit:
I see now that I was quite unclear though. And I do understand how to manually allocate memory and stuff. But the point is that the data variable actually consists of the price and the product. And that is where I'm stuck since I don't know what and what not to allocate for memory.
Does anyone know what I'm doing wrong? I hope if I've been clear enough, if not let me know. Thanks in advance :)