1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

urgent help in linked list and strstr

Discussion in 'C' started by ilovec.., Nov 3, 2006.

  1. ilovec..

    ilovec.. New Member

    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 a moderator: Nov 3, 2006
  2. shabbir

    shabbir Administrator Staff Member

    What does ch1 contains in the line found = strstr( ptr->task,ch1 );
     
  3. ilovec..

    ilovec.. New Member

    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: Nov 3, 2006
  4. shabbir

    shabbir Administrator Staff Member

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

    ilovec.. New Member

    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: Nov 3, 2006
  6. shabbir

    shabbir Administrator Staff Member

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

    ilovec.. New Member

    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: Nov 3, 2006
  8. ilovec..

    ilovec.. New Member

    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");
     
  9. shabbir

    shabbir Administrator Staff Member

    You are not allocating the found pointer.
     

Share This Page