Hi,
I was reading a text file,and storing each word in a struct:
and i got a function makeWordNode function to create a word nodeCode:typedef struct WordNode{ struct wordNode *nextWord; char *word; //store the word int whiteSpaces; //num of spaces between words }wordNode;
interesting this code wont work and crashes.so i added printing of # char to check where the code hangs:Code:wordNode *makeWordNode(char* string,int numSpaces){ wordNode *newWordNode; newWordNode=(wordNode*)malloc(sizeof(wordNode)); strcpy(newWordNode->word,string); newWordNode->whiteSpaces=numSpaces; newWordNode->nextWord=NULL; printf("#%s->%d#",newWordNode->word,newWordNode->whiteSpaces); return newWordNode; }
Interestingly the 2nd version works fine and prints correct output.I cant figure out the reason for this wierd error.Code:wordNode *makeWordNode(char* string,int numSpaces){ wordNode *newWordNode; newWordNode=(wordNode*)malloc(sizeof(wordNode)); printf("#"); strcpy(newWordNode->word,string); newWordNode->whiteSpaces=numSpaces; newWordNode->nextWord=NULL; printf("#%s->%d#",newWordNode->word,newWordNode->whiteSpaces); return newWordNode; }
help me!! Thanks



LinkBack URL
About LinkBacks


