Originally Posted by
quzah
Ah, well that's simple. You aren't passing a pointer-to-pointer for your replace_words function. You can't change what the first node points to, because the replacement is lost when the function ends, because you can't change that pointer.
Quzah.
That makes sense... it also made a little light click on in my head.
Currently when I'm replacing words I free() my string and then the node which is completely unnecessary I'm assuming. All I need to free is the string and then malloc another the size of what I need... doh.
After dinner I'll get on that but I'm fairly certain it'll work.
Thanks for your help!
-Peter
**EDIT**
Yeah that did it. That original monstrocity is now cut down to this.
Code:
int replace_words(WordNode *words, const char *oldword, const char *newword)
{
WordNode *temp = find_word(words, oldword);
while(temp)
{
free(temp->word);
temp->word = (char *)malloc(strlen(newword) + 1);
if(!temp->word)
return ERROR;
strcpy(temp->word, newword);
temp = find_word(words, oldword);
} /* end while(temp) */
return 0;
}
Thanks again, seems like it always ends up being that I try and make things harder than they really are.