The problem is here:
for (i = 0; i < argc; i++) {
node next = node_new(argv[i]);
cur->next = &next;
cur = &next;
}
By allocating next
like this, it remains tied to the stack and doesn't actually change address on each iteration. It should be a new object each time:
for (i = 0; i < argc; i++) {
node *next = malloc (sizeof node);
next->word = argv[i];
next->next = NULL;
cur->next = next;
cur = next;
}
Also, node_new()
can't be used because it doesn't allocate any lasting new memory either.