Q: How to initialize a struct into a pointed struct Namaste! I want to initialize my struct array position 0 myList.items[0] with a pointer to my item struct, but it prints out jibberish on the relevant positions when I print it out from my print function. It changes what was previously initialized (for test) so I know it works partially, but what's causing the bad output and what should be changed?
Before: 
----My Shopping list---------
1 - Chocolate   40      100g
2 - Fishsauce    9       l
After:
----My Shopping list---------
1 - c┴®¶²`       128565603       ■   lüIv
2 - Fishsauce    9       l
typedef struct{
        char name[20];
        int amount;
        char amountType[10];
}item;

typedef struct{
        item *items[5];
        int length;
}list;

int addItemToList(list *myList);

main(void)
{
    list myList;
    myList.length = 0;
    for(int i; i<5;i++)
    {
        myList.items[i] = NULL;
    }
    addItemToList(&myList);
    return 0;
}


int addItemToList(list *myList)
{
    item newItem = {"Potatoes",2, "kg"};
    myList->items[myList->length]=&newItem; //Something wrong here?
    myList->length++;
    printf ("Added [%s %i %s] as #%i.", newItem.name, newItem.amount,newItem.amountType, myList->length);
return 0;
}


A: This:
int addItemToList(list *myList)
{
    item newItem = {"Potatoes",2, "kg"};

That allocates newItem on the stack.  That means the memory for it will go away when addItemToList is completed, so &newItem will be pointing to gibberish later.  It will be fine while still running code in addItemToList, but then after that the memory contents will be replaced in any further functions that are called.
You can either use malloc to allocate some memory for newItem, or you can allocate newItem on the stack in your main function and pass the pointer to newItem to any other called functions.

A: You cannot initialize a struct on the stack and then pass the pointer to that struct to a different struct that lives longer than that stack frame is open. Once the function returns, the stack frame where your item was allocated will close and the assigned values to the fields will be gone.
You need an initializer function with a heap allocated struct like this:
item* item_new(const char* name, int amount, const char* amountType);

Then your initialization function should:


*

*call Malloc for the size of struct,

*Copy the name and amountType strings to the struct.

*copy the amount to the struct.


Then you can do this: myList->items[myList->length]=newItem;, where newItem is created by your init function.

A: You are mixing stack and heap memory, I rewriten it for you, it should give you some sense what went wrong. Also always free allocated memory its no java:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
        char name[20];
        int amount;
        char amountType[10];
}item;

typedef struct{
        item *items[5];
        int length;
}list;

int addItemToList(list *myList);

int main(void)
{
    list myList;
    myList.length = 0;
    for(int i; i<5;i++)
    {
        myList.items[i] = 0;
    }

    addItemToList(&myList);

    for(int i; i<myList.length; i++)
    {
        free(myList.items[i]);
    }

    return 0;
}


int addItemToList(list *myList)
{
    item* newItem = malloc(sizeof(item));
    strcpy(newItem->name, "Potatoes");
    newItem->amount = 2;
    strcpy(newItem->amountType, "kg");
    myList->items[myList->length++] = newItem; //Something wrong here?
    printf ("Added [%s %i %s] as #%i.\n", newItem->name, newItem->amount, newItem->amountType, myList->length);
    return 0;
}

