Thread: help with strstr() function

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    help with strstr() function

    hey everyone,
    im having trouble with strstr(), and i'm not sure what's happening,
    but i get a nice segmentation fault.
    here is parts of my code:

    Code:
    char input[BUFFER],  temp[BUFFER], *result;
    
    /*get input from user, that's fine*/
    ...
    
    
          result = strstr(temp, input)
    
          printf("\ninput = %s\n", input); 
          printf("temp = %s\n", temp);      
          printf("result = %s\n", result);    
    
          if(result != NULL)
          {
             printf("this is result = %s\n", result);
    	 break;
          }
    
          else
          {
             printf("no\n");
          }
    the segmentation fault occurrs when i try to print result.
    is there something obviously wrong there? most likely.
    any help would be great.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You attempt to print result before checking whether it is NULL. Don't do that.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    ok, i tried putting that on the end - segemntation fault.
    then i deleted it, and it printed error, even when i entered a match. printing that isnt important, it will be deleted entirely when it all works, i just wanted to see what was working and what wasn't.
    but when result isn't NULL shouldn't it print result in the if statement???

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zacavitch View Post
    but when result isn't NULL shouldn't it print result in the if statement???
    It does.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    well that's what i thought, but i get a segmentation fault.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So this works:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(void) {
        char temp[80] = "tester tester tester";
        char input[80] = "r test";
        char *result;
    
        result = strstr(temp, input);
        if (result != NULL) {
            printf("this is result = %s\n", result);
        }
        return 0;
    }
    If it doesn't work for you then you've managed to break strstr. If it does work for you, then your code is broken.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    I'm guessing the problem is somewhere in this code of yours:

    Code:
    /*get input from user, that's fine*/
    Ummm hmmm.

    Is it possible one of your strings isn't null terminated? Maybe 'temp'?

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    that code did work for me.

    well I get temp from

    Code:
    strcpy(temp, head);
    so maybe it's not nullified...?


    So is:
    Code:
    temp[strlen(temp)] = NULL;
    a good way to make it NULL?

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by zacavitch View Post
    So is:
    Code:
    temp[strlen(temp)] = NULL;
    a good way to make it NULL?
    No. Two problems: First, strlen() requires a string. If there's no null terminator, then calling strlen() is wrong. If there is a null terminator, you don't need to add one (you'd just be replacing a null terminator with a null terminator.) Second, NULL is not the null character; it's a null pointer constant. 0 or '\0' is a null character.

    strcpy() adds a null character to the target. If the target is large enough for the string to be copied into, and the source is really a string (that is, it has a null terminator), then there are no problems with the strcpy().

    It's guesswork until you post all your code.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    So here is my entire function...BUFFER = 100 and TITLE_LEN = 40.
    Code:
    void findBooks(BookListType* booklist)
    {
       BookType *currentBook = booklist->head;
       int i;
       char sel[BUFFER], input[TITLE_LEN], *result,
       temp[BUFFER];
       
       printf("\nSearch for a book title (1-40 characters):");
    
       fgets(input, TITLE_LEN, stdin);
    
       
       if(input[strlen(input)-1] > TITLE_LEN)
       {
          printf("\nToo many characters entered, returning to menu...\n\n");
          return;
       }
     
       printf("\n\nID Title                           Author        Year Price  Count\n");
       printf("-- ------------------------------- ------------- ---- ------ ------\n");
       
       while(currentBook != NULL)
       {
          strcpy(temp, currentBook->title);
          
          for(i=0; input[i]; i++)
          {
             input[i] = tolower(input[i]);
          }
          
          for(i=0; temp[i]; i++)
          {
             temp[i] = tolower(temp[i]);
          }
      
          result = strstr(temp, input);    
          if(result != NULL)
          {
             printf("this is result = %s\n", result);
    	 printf("%d %s", currentBook->id, currentBook->title);
    	 printf("%s %d", currentBook->author, currentBook->year);
    	 printf("$%f %d\n", currentBook->price, currentBook->count);
          }
              
    
          currentBook = currentBook -> next;
       }
    }

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you have new-line characters at the end of your stored titles to match the \n character at the end of temp?

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    i don't think i do, the titles are read from a file using strtok seperated by ":"

    so it would make sense that they don't, how can i add one and keep a terminator as well?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The idea was that you would take the \n off the input.

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    hey I got it working, i took the "\n" off the input and that did it,
    and as an aside took the input tolower part out of the loop as well.

    Thanks heaps for your help everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM