urgent help in linked list and strstr

ilovec..'s Avatar, Join Date: Nov 2006
Light Poster
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 input
      node *ptr1;                        //pointer
      ptr1 = search( list, find);    // sends in the list and array 'find'
      if( ptr1 ==NULL ) {            //if ptr null means nothing found  
         printf("\n");
      }
      else  {                             //something found, print node
         printCurrent(ptr1);    
       }
------------------------
Code:
node * search(node *ptr, char *find)    //takes in ptr as head and array
{      

  node *target;                        
   void *found;                        
   while( ptr!=NULL)   {                          //run through list till null
       found = strstr( ptr->task,ch1 );       // assign found to whatever strstr comes up with 
       if(found !=NULL){                          //found is something other than null
       target = ptr;
       return (target);                            //return pointer to the node to be printed
   }else{
       ptr = ptr->next;}                       //nothing found, cycle to next node
      found = NULL;                         //reset found
   }
return 0;
}
the problem is strstr never picks up find[] in my ptr->task. i even made them identicle. any help would be appreciated. thank alot in advance.

Last edited by shabbir; 3Nov2006 at 12:04.. Reason: Code formating.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
What does ch1 contains in the line found = strstr( ptr->task,ch1 );
0
ilovec..'s Avatar, Join Date: Nov 2006
Light Poster
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.

Last edited by ilovec..; 3Nov2006 at 13:42..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
ilovec..'s Avatar, Join Date: Nov 2006
Light Poster
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.

Last edited by ilovec..; 3Nov2006 at 17:32..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
ilovec..'s Avatar, Join Date: Nov 2006
Light Poster
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.

Last edited by ilovec..; 3Nov2006 at 18:19..
0
ilovec..'s Avatar, Join Date: Nov 2006
Light Poster
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");
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You are not allocating the found pointer.