![]() |
urgent help in linked list and strstr
Hi, i need some quick help in something ive been working on and cannot seem to get around. any help would be appreciated.
i have a linked list. singly. each node has a 2 strings, an int, and ofcourse next. i also have an array, say find[], which contains a string of words. the aim is to run through the linked list and search in every node->string if find[] exists. for instance, node->string could be 'hello world' and find[] could just be world. That is, it doesnt have to be an exact match. once it find[] exist in the string, it should print the node and move on to the next node. i decided to use strstr since it does the job. However im having a problem with what it returns. below is the brief code. Code:
fgets( find, MAX_LINE, stdin ); //scans inputCode:
node * search(node *ptr, char *find) //takes in ptr as head and array |
Re: urgent help in linked list and strstr
What does ch1 contains in the line found = strstr( ptr->task,ch1 );
|
Re: urgent help in linked list and strstr
sorry...ch1 is find....its the array of what has to be found. typo but i changed the name from ch1 to find to make it easier to read here. its correct in the program...its the array.
|
Re: urgent help in linked list and strstr
What are the values of both the params in strstr and what is the output can you post that here so that we can see into it with more details.
|
Re: urgent help in linked list and strstr
the node struct is as follows.
struct node { char *task; int i; char *cht; node *next; }; so find[] should look into node->task and see if there is a match. if not, go to the next node. so lets say find[] = match and node->task = this has to match with find then the output should be task: this has to match with find. (basically print out the node.) if the next node->task also has 'match', then that should print too. as for your questiion, task is basically a pointer to a string and find[] is just an array. so im passing in a pointer too...or so i think. |
Re: urgent help in linked list and strstr
If you write
char* found = new char[100]; found = strstr( "this has to match with find","match" ); you will see found as match with find so I guess you need to see if the found is allocated any buffer before you assign. |
Re: urgent help in linked list and strstr
so i change the *void found to *char found. also i know the max match can only be 128 characters so *char found [128]? then
*char found [128] = strstr.... how do i see if found has allocated any buffer? do i malloc? thanks. |
Re: urgent help in linked list and strstr
i changed my code so i didnt have to use a function...having the same problem as above...pls help. thanks a tonne
printf("Search Text: "); char ch1[MAX_LINE]; fgets( ch1, MAX_LINE, stdin ); node *ptr; ptr = list; //assigns ptr to head of list while(ptr != NULL){ // go through list char *found; //assign found found = strstr(ptr->task,ch1); if(found != NULL){ printCurrent(ptr); //prints the current node } ptr = ptr->next; //go to next node } printf("\n"); |
Re: urgent help in linked list and strstr
You are not allocating the found pointer.
|
| All times are GMT +5.5. The time now is 16:51. |