Thread: segmentation fault??

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    15

    segmentation fault??

    i'm trying to write a program that accepts three strings: text, s, and r. The function searches string text for string s. if it finds string s, it should replace it with string r. i wrote all the code and it compiles but when i try and run it stops and says "segmentation fault"

    i know it's something with my pointers but i can't figure it out for the life of me. i could definitely use some help...thanks

    Code:
    #include <stdio.h>
    #include <string.h>
            
    #define MAX 100
    char search_and_replace(char *text,char *s, char *r);
             
    int main(void)
    {                
            
            char *text[MAX];
            char *s[MAX];
            char *r[MAX];
     
            /* Find text string */
            printf("Please enter a text string.\n");
            gets(*text);
    
            /* Find the search string */
            printf("Please enter a search string.\n");
            gets(*s);
            
            /* Find the replacing string */
            printf("Please enter a replace string.\n");
            gets(*r);
           
            search_and_replace(*text, *s, *r);
                     
            return(0);
            }
                     
    char search_and_replace(char *text, char *s, char *r)
    {
    
    int next;
    char length_text;
    char length_s;
    char diff;
    
            
            length_text = strlen(text);
            length_s = strlen(s);
            diff = length_text - length_s; 
            
            for(next = 0; next <= diff; ++next);
            {
                    if(strncmp(&text[next], s, length_s) == 0)
                    {
                    strcpy(&text[next], r);
                    printf("%s", text);
                    }
            }
     
    return(0);
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. don't use gets - see FAQ
    2. char *text[MAX]; is just an array of pointers to char that point nowhere
    *text is a first pointer, that points nowhere... when you try to write something (using gets) to unknown location - behaviour is undefined. 2 most probable results - you get an protaction fault due to the random address is outside your address space - or you overwrite some valuable data of your program that will cause the crash when you read this data.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    i'm still confused as to how to get rid of the segmentation fault. if i take out the pointers, the program doesn't compile

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should write to the memory you own
    if you want to store several strings - use 2-dimentional array, not array of pointers

    or dynamically allocate memory fro every pointer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    i try and just use scanf to take the strings but it gives me the same segment error

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by snappleapple View Post
    i try and just use scanf to take the strings but it gives me the same segment error
    You try what? post your new code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    I'm really really confused by c's little technicalities. when i use scanf like this it lets me enter in the first string but not the second and third strings

    Code:
    int main(void)
    {
      
            char *text[MAX];
            char *s[MAX];
            char *r[MAX];
      
            /* Find text string */
            printf("Please enter a text string.\n");
            scanf("&#37;s", &text);
       
            /* Find the search string */
            printf("Please enter a search string.\n");
            scanf("%s", &s);
    
            /* Find the replacing string */
            printf("Please enter a replace string.\n");
            scanf("%s", &r);
    
            /*Performs program task*/
            search_and_replace(*text, *s, *r);
    
            return(0);

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Remove the stars, Luke.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    getting rid of the stars gives me this:

    Code:
    kkeyser@shellfish:~/CPrograms> gcc search-replace.c
    search-replace.c: In function `main':
    search-replace.c:40: warning: passing arg 1 of `search_and_replace' makes pointer from integer without a cast
    search-replace.c:40: warning: passing arg 2 of `search_and_replace' makes pointer from integer without a cast
    search-replace.c:40: warning: passing arg 3 of `search_and_replace' makes pointer from integer without a cast

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    All of them in main

    Like
    char s[MAX];

    fgets( s, sizeof s, stdin );

    myfunc( s );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM