-1

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 :)

  • 7
    Either you have been asleep in class, you you need to [get a couple of good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), because there are so many problems with those few lines of code. – Some programmer dude Dec 18 '17 at 19:07
  • 2
    1. You don't need that `struct data`, operate directly on `product_t`. 2. `sizeof(data)` doesn't do what you think it does. It's the size of a pointer, probably 4 or 8 bytes. You want to use `strlen()` instead, (or even `strdup()` instead of manual malloc'ing). – HolyBlackCat Dec 18 '17 at 19:13
  • 1
    Why would you use a separate `struct product_t*` for each field of the `data` structure? – Barmar Dec 18 '17 at 19:36
  • 1
    If you write "I get errors" , please , please copy paste those errors so the people that want to help you can see those errors. Error messages are normally very informative and invaluable. – nos Dec 18 '17 at 19:41
  • `sizeof(char)*sizeof(data)+1` makes no sense. Either it should be `sizeof(char)*(sizeof(data)+1)`, or (better) drop the `sizeof(char)*` part. `sizeof(char)` is 1 by definition. – melpomene Dec 18 '17 at 19:48

2 Answers2

1

Start with something like this:

#include <stdlib.h>
#include <string.h>

struct product_t
{
    char* data;
    int   id;
};

struct product_t* createProduct(char* data, int id)
{
    struct product_t *product = malloc(sizeof(struct product_t));

    product->data = strdup(data); // Assumes that data is a \0 terminated string.
    product->id   = id;

    return product;
}

void deleteProduct(struct product_t* product)
{
    free(product->data);
    free(product);
}

To do:

  • Add checks to make sure that data is not NULL.
  • Add code for for a linked-list of products.
  • You may need to add casts, but the compiler will tell/warn you.
  • ...

Groetjes en succes!

melpomene
  • 84,125
  • 8
  • 85
  • 148
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • 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. – Jurjen Kuijstermans Dec 18 '17 at 21:24
  • Well, that's indeed a whole other story which you don't explain nor ask about. – meaning-matters Dec 19 '17 at 09:50
0

I guess that you really wanted to do the following:

struct product_t {
    char *product;
    double price;
    int id; 
    struct product_t *next;
};

struct product_t *createProduct(char *name, double price, int id) {
    struct product_t *New = (product_t*) malloc(sizeof(struct product_t));
    New->product = (char*) malloc(strlen(name)+1);
    strcpy(New->product, name);
    New->price = price; 
    New->id = id; 
    return New;
}
Serge
  • 11,616
  • 3
  • 18
  • 28