I have a function that create a linked list of struct.
struct words {
char * word;
words next;
};
typedef struct words s_words;
typedef words * Words;
I got a function that create my linked list using this code
Words w = NULL; // start of the list
Words t; // temp node
if (t = (Words) malloc(sizeof(s_words))) {
t->word = a_string_created;
t->next = w;
w = t; // Adding at start of the list
}
If I made a printf("%s",t->word) and printf("%s",a_string_created) I got the same value.
My problem is when I try to retrieve word from an another function.
int readWords(Words list, char * wordRead) {
if (list != NULL) {
//strcpy(wordRead,list->word);
wordRead = list->word;
return 1;
}
else {
return 0;
}
}
I can't get the value in readWords. A printf("%s",list->word) in it give me a strange caracter. And from the caller function
char rw[11]; //I've try char* rw too
readWords(aList,rw);
printf("%s",rw) print nothing.
I'm stuck with this for hours now. For sure there's something I don't see/understand.
Edit:
I solved partly my problem by replacing t->word = a_string_created; by strcpy(t->word, a_string_created); Now on my printfs, I print string value. But the value have slightly change for some values ex.: test become uest !!
Answer
Change t->word = a_string_created; to t->word = strdup(a_string_created);
Anyone can help and explain to me where and why I'm wrong ?