I'm trying to implement my own linked-list like structure in C, however I'm failing even with easiest part of just adding new elements to the list. Adding one element to the list and printing it works fine, adding another and printing the one doesn't. After executing it the console simply outputs "memory error" - that's it. I'm pretty sure I've messed up with the iteration of the current-pointer in my insert_list function, but I can't find where.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 80
typedef struct list
{
struct list *next;
char value[MAXLEN];
} list;
void insert_list(list *lis, const char string[MAXLEN])
{
list *current = lis;
while(current->next)
{
current++;
}
current->next = malloc(sizeof(lis));
strcpy(current->next->value, string);
}
int main(void)
{
list lis =
{
NULL,
"Hello1"
};
insert_list(&lis, "Hello2");
insert_list(&lis, "Hello3");
/* This still works */
printf("%s %s", lis.value, lis.next->value);
/* This doesn't */
printf("%s", lis.next->next->value);
return 0;
}