I'm trying to implement a doubly linked list in C and I need to use an initialize function, while maintaining a size field. My code is as follows:
typedef struct element{
struct element* next;
struct element* prev;
int value;
}element_t;
typedef struct linkedlist{
element_t* head;
element_t* tail;
int size;
}linkedlist;
void init(linkedlist* list){
list = malloc(sizeof(linkedlist));
list->size = 0;
}
int main(int argc, char** argv){
linkedlist* list;
init(list);
return 0;
When I'm trying to access list->size in the init function, I get the correct value, But when I try to access list->size from main the program returns a strange, large negative value (probably an address in hex).
Would like to know what I'm doing wrong. stdlib is included.