1

I have a struct like this:

typedef struct stockItem {
    char *componentType;
    char *stockCode;
    int numOfItems;
    int price;
} stockItem;

// declaration
stockItem *stockItem_new(char *componentType, char *stockCode, int numOfItems, int price);

And a struct like this to store many stock items ( linked list )

typedef struct inventory {
    struct stockItem item;
    struct inventory *next;
}inventory;

These are both in different header file.

I have created the linked list, I want to print off certain bits of data, such:

void outputData(){

    // This temporarily takes the location of the structs in the
    // linked list as we cycle through them to the end

    struct inventory *myInv = pFirstNode;

    printf("Current Inventory\n\n");

    // Until the ptr reaches a value of NULL for next we'll
    // keep printing out values

    while(myInv != NULL){
        // HERE IS MY PROBLEM HOW DO I PRINT OFF THE COMPONENTTYPE FROM THIS
        printf("%s\n\n", myInv->item->compnentType);
        // Switch to the next struct in the list
        myInv = myInv->next;
    }
}

EDIT:

stockItem *stockItem_new(char *componentType, char *stockCode, int numOfItems, int price){
   // creates a new duration for the song
   stockItem *item = (stockItem*)malloc(sizeof(stockItem));

   // assigns the attributes
   item->componentType   = componentType;
   item->stockCode = stockCode;
   item->numOfItems = numOfItems;
   item->price = price;
   // returns it
   return item;
}
  • We need to see the code of `stockItem_new` – David Ranieri Mar 28 '16 at 16:38
  • Certaininly @AlterMann –  Mar 28 '16 at 16:39
  • Don't cast the result of `malloc` in C, also why do you have a `stockItem_new` (which returns a pointer) if you don't keep a pointer in the `inventory`? (with that declaration, it would be `item.compnentType` since `item` is not a pointer to `stockItem`). – crashmstr Mar 28 '16 at 16:51
  • I made the stockItem before the inventory so I guess I wasn't foward thinking. What do you mean don't cast the result of malloc in c? @crashmstr –  Mar 28 '16 at 16:57
  • What does the compiler tell you when you try to compile the code in the question? The `myInv->item->componentType` is a `char`, but you need a `char *` to print with `%s`, so simple changing it to `myInv->item.componentType` could just do it. – Honza Remeš Mar 28 '16 at 16:58
  • [don't cast the result of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – crashmstr Mar 28 '16 at 16:58
  • This is what I get @HonzaRemeš ݲ` ݲ` ݲ` ݲ` ݲ` ݲ` ݲ` ݲ` ݲ` –  Mar 28 '16 at 17:02
  • @session_start: That is maybe what your program prints. What I am asking is: "What messages do you get when compiling the code?" – Honza Remeš Mar 28 '16 at 17:03
  • You probably need to change `inventory` to be a `struct stockItem *` to work with your `stockItem_new` (but you *don't* show us enough code to know for certain). – crashmstr Mar 28 '16 at 17:06
  • @crashmstr perfect mate, changed it to struct stockItem * and i can access with this code printf("%s\n\n", myInv->item->componentType); Make an answer and and I'll accept –  Mar 28 '16 at 17:09

1 Answers1

1

We don't see the rest of the code, but since you have stockItem_new returning a pointer, then this is wrong:

typedef struct inventory {
    struct stockItem item; ///missing *!
    struct inventory *next;
} inventory;

Instead, we need to declare it as:

typedef struct inventory {
    struct stockItem *item;
    struct inventory *next;
} inventory;

Then you can assign to the item with your stockItem_new function and your outputData will work as you expect.

Update: In your stockItem_new, you are not making a copy of the contents of componentType, but just pointing to the same value. You either need to allocate a new buffer each time and pass into stockItem_new or take care of that with strdup

item->componentType = strdup(componentType);

This will allocate enough memory and copy the contents of componentType. You would need to do this for any strings you will be keeping in your struct (since only the address is copied!).

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • Thanks for the answer, another quick question. I'm printing out the data and for some reason I am getting the same stock code for each item in the inventory but the ints are working fine ( price, count ) any ideas? I'm guessing it has something to do with the pointers –  Mar 28 '16 at 18:21
  • You are pointing to the same buffer each time. In your `stockItem_new` you could use [strdup](http://pubs.opengroup.org/onlinepubs/009695399/functions/strdup.html) to allocate new storage with a copy of the string. – crashmstr Mar 28 '16 at 18:24
  • Am I not doing that with stockItem *item = malloc(sizeof(stockItem)); –  Mar 28 '16 at 18:25
  • That is creating an "empty" item. When you do this `item->componentType = componentType;`, you point to the same thing as `componentType`, and it that later changes (due to reading a file, etc.), then they *all* change. – crashmstr Mar 28 '16 at 18:26
  • So I'd need to use strdup for all of them ? –  Mar 28 '16 at 18:27
  • @session_start I've updated my answer. You only need to do that with strings (since they are pointers). – crashmstr Mar 28 '16 at 18:30
  • Ahh thats brilliant, could this be done a different way? by dereferencing? –  Mar 28 '16 at 18:36
  • @session_start You might want to read up on string handling and learn more about pointers. If you had a pointer to an `int`, you would dereference it to get the value. Since strings are arrays of values, you don't work with them in the same way. – crashmstr Mar 28 '16 at 18:41
  • Absolutely I'll have a read into it, thank you again. Say if I were to have a file which brings up all the sales ( date, stock code, amount) is it possible to have a function call in a struct so I can have the StockItem within the sales struct if that makes sense? –  Mar 28 '16 at 18:56