malloc is used to allocating memory and return a (void *).
You should return a pointer of type struct list from malloc.
so
-
struct list *s = (struct list *)malloc(sizeof(struct list));
-
I think the compiler will do this pointer conversion implicitly. The question was not formed properly. This is more more accurate description of the problem
-
int llistAdd(LList list, char const *text){
-
-
LListNode *new=(LListNode *)calloc(1,sizeof(LListNode));
-
-
new->text=malloc((strlen(text)+1)*sizeof(char));
-
strcpy(new->text, text);
-
-
73: if(llistCount(list)==0){
-
74: list->first_node=new;
-
75: list->last_node=new;;
-
76: return 1;
-
}
-
...
-
gives a Valgrind error
Invalid write of size 4
==6423== at 0x80488A1: llistAdd (llist.c:75)
==6423== by 0x80484DB: main (main.c:20)
==6423== Address 0x417F02C is 0 bytes after a block of size 4 alloc'd
==6423== at 0x4021AA4: calloc (vg_replace_malloc.c:279)
==6423== by 0x8048795: llistConstruct (llist.c:20)
==6423== by 0x80484B9: main (main.c:17)
no error on line 74 though. Similar problem occurs when assigning NULL to a freed variable.