hi, i got a problem with my link list...at the last while loop, i realise dat currNode->next is null pointer. can anyone help me with this? thanks
hi, i got a problem with my link list...at the last while loop, i realise dat currNode->next is null pointer. can anyone help me with this? thanks
Last edited by pothios; 03-15-2008 at 12:14 AM.
Maybe the first of these:
should actually be pointing to another node?Code:currNode->next = NULL;
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
currNode->next = NULL;
i thought i should set the next node to null since its the last node?
But you have [unless my eyes are playing tricks] two of those lines, the first one, I think should be something else.
There are also two other problems:
1. You are storing the same address for every single word, so your linked list will have the last word every time.
2. You are allocating an extra node at the end of the list which isn't being used.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
how do i solve the adress problem?
You've presented a code fragment, which makes things a bit more difficult, but this is close (and untested):
Code:while(fgets(entry, sizeof(entry) , myfile)!=0) { // Create a new node currNode = malloc(sizeof(*currNode)); // Allocate enough memory to hold the string you've just read currNode->words = malloc(strlen(entry) + sizeof(*entry)); if (!currNode->words) { // Bail out of the prog altogether as it's likely all gone to hell exit(EXIT_FAILURE); } // Copy the string read in to the newly-allocated memory strcpy(currNode->words, entry); currNode->next = NULL; tailNode = currNode; }
Last edited by rags_to_riches; 03-14-2008 at 08:17 PM. Reason: Missed a /
You may have glanced at my code, but you didn't read it.
The first time you hit currNode in the while loop...what is it pointing to?
nvm got it
Last edited by pothios; 03-15-2008 at 12:39 AM.
Please don't edit your original post to remove most of it.
That makes what other people have posted look like nonsense. It also stops others from being able to learn from your mistakes.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"