I'm new to C and new to programming as well and I've just started looking at linked lists.
static struct post {
char * str;
struct post * next;
}
head = {0, NULL};
int stringdb_add(const char * str) {
int pos = 0;
struct post * new_input = (struct post * ) malloc(sizeof(struct post));
new_input - > str = (char * ) malloc(strlen(str) + 1);
if (head.next == NULL) {
strcpy(new_input - > str, str);
new_input - > next = NULL;
head.next = new_input;
} else {
while (head.next - > next) {
++pos;
head.next = head.next - > next;
}
strcpy(new_input - > str, str);
new_input - > next = NULL;
head.next - > next = new_input;
}
return pos;
}
The function "stringdb_add" is supposed to return the position the new node has been placed in, but when I test the function I only get (00111111....).
This is probably because the list never links properly.