Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   urgent help in linked list and strstr (http://www.go4expert.com/forums/urgent-help-linked-list-strstr-t1792/)

ilovec.. 3Nov2006 11:48

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 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.

shabbir 3Nov2006 12:07

Re: urgent help in linked list and strstr
 
What does ch1 contains in the line found = strstr( ptr->task,ch1 );

ilovec.. 3Nov2006 13:40

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.

shabbir 3Nov2006 16:43

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.

ilovec.. 3Nov2006 17:20

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.

shabbir 3Nov2006 18:12

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.

ilovec.. 3Nov2006 18:16

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.

ilovec.. 4Nov2006 11:32

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");

shabbir 6Nov2006 10:40

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 17:12.