segmentation fault??

This is a discussion on segmentation fault?? within the C Programming forums, part of the General Programming Boards category; i'm trying to write a program that accepts three strings: text, s, and r. The function searches string text for ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21