Newing up a struct like OOP in C
I was having a very bad time writing a new up function in C. I didn't understand why I could return the pointer out of the function when I ran the structure cast on the struct. The problem is I forgot to de-reference the pointer for assignment.
todo_item *out = malloc(sizeof(todo_item)); out = (todo_item) etc.
Bad code, this doesn't work because I'm assigning a non-pointer cast to a pointer that isn't de-referenced. Here's another bad thing I tried:
todo_item *out = malloc(sizeof(todo_item)); out = &(todo_item) etc.
This isn't correct, I believe this takes the local reference of the todo_item cast and if you return out you are returning a local item out of scope.
todo_item *out = malloc(sizeof(todo_item)); //De reference and then assign the cast: *out = (todo_item) etc.
I struggled with this for awhile, I hope this is helpful to someone.