Malloced global variable implementation (C) -


i make linked list globally available across multiple .c files.

i've read how can't identify causing problem.

i declare variable extern in linkedlist.h:

extern linkedlist* canqueue; 

and in main.c initialise variable sending function in linkedlist.c so:

linkedlist *canqueue=createlist(); 

this create function in linkedlist.c:

linkedlist* createlist() /*creates empty linked list*/   {     linkedlist* mylist;     mylist = malloc(sizeof(linkedlist));     mylist->head = null;     return mylist;   } 

i want use canqueue in file, cpu.c. i've included linkedlist.h in cpu.c, @ point linked list should available here know. when try access error:

undefined reference 'canqueue' 

have missed or done wrong?

it seems don't define such global variable. if code compiles:

linkedlist *canqueue = createlist(); 

then it's not "global" (file-scope) variable. define @ file scope , initialize don't shadow local variable. in all, this:

// @ global scope linkedlist *canqueue;  void initialize() // or whatever {     canqueue = createlist(); } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -