Linked List in C error -- Invalid Initalizer error -


i attempting create linked list using c (not c++). linked list initialized through function llinit() should return list struct. however, when compile code error stating "error: invalid initializer" in lltester.c file. why this?

this function used initialize linked list in llist.c file:

list llinit() {     list* ll = malloc(sizeof(list));     ll->head = null;     ll->tail = null;     return *ll; } 

this list struct in llist.h file:

typedef struct {     node *head;     node *tail; } list; 

this main function in lltester.c file attempt initialize list:

int main() {     list mylist= llinit(); //this line error occurs on!      return 0; } 

your code leaking memory, since it's allocating list malloc() returning structure's content by value.

if want have function returns empty list value, should that:

list llinit() {     list ll;     ll.head = null;     ll.tail = null;      return ll; } 

the above fine, there's no risk of value "disappearing" due going out of scope, safe e.g. function returning local int value. return value (all of it!) copied caller's memory needed.

it's trivial caller decide whether or not node should on heap or not.

update: here code on ideone, compile , not generate error assignment in main().


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -