Thread: Segmentation fault? Strings

  1. #1
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18

    Question Segmentation fault? Strings

    This program compares two strings and outputs however many times the letter is found in the word the user inputs. I'm having a problem with it getting a segmentation fault and can't figure out why? Am I somehow performing a buffer overflow with this code?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int num_appearances(char word[], char letter);
    
    
    int main()
    { 
        char wordstring[20];
        char searchLetter;
        
        printf("Please enter the word you would like to scan:\n");
        scanf("%s", &wordstring);
        
        printf("Please enter a letter to search for:\n");
        scanf("%s", &searchLetter);
        
        int num_times = num_appearances(wordstring, searchLetter);
        
        if(num_times > 0){
                     printf("The letter %s appears %d times in the word %s\n", searchLetter, num_times, wordstring);
        }
        else
            printf("The letter %s appeared zero times in your word\n");                                        
        system("PAUSE");
        return 0;
        
    }
    
    
    int num_appearances(char word[], char letter){
        int i;
        int counter = 0;
        for(i=0; i != '\0'; i++){
                 if(strcmp(&word[i], &letter)==0){
                            counter++;
                 }
                 else
                     continue;      
        }
        
        return counter;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%s", &searchLetter);
    Yes, this is always a buffer overflow.

    If you want to read the next non-space letter, then use
    scanf(" %c", &searchLetter);

    More train-wrecks here
    Code:
        for(i=0; i != '\0'; i++){
                 if(strcmp(&word[i], &letter)==0){
    I suggest you think about it, rather than just throwing in some & all over the place to shut the compiler up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18
    Thanks thats what I love about programming. You learn something new everyday. I'll fix this pronto, but If I'm trying to compare two string characters doesn't the strcmp() function require you to pass by reference? Thats why I passed word[i] and letter that way. Thanks again

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    strcmp() is for comparing two strings, but in your for-loop I think you want to compare two characters. For comparing two characters you just use the "==" operator.

    Bye, Andreas

  5. #5
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18
    Ok Thanks I appreciate that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (strings, pointers, Linux)
    By ankushwrites in forum C Programming
    Replies: 2
    Last Post: 03-25-2012, 12:00 PM
  2. Unable to join two strings due to Segmentation Fault
    By Orange Lozenge in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2011, 04:00 PM
  3. Segmentation fault when appending to strings (char *)
    By LanguidLegend in forum C Programming
    Replies: 15
    Last Post: 02-24-2011, 07:35 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread