Thread: "Segmentation Fault" Substring replace in arr of char *

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    6

    "Segmentation Fault" Substring replace in arr of char *

    Hello Everyone, I got this question in some book. As per question we need to find a substring in a array of char* and replace it with another string. The program I wrote doesn't work. I tried using strcpy as well char by char.
    What I know is "char ptr" is "ptr to const" or we can say literal in readonly section in memory. So we can't modify the string it points to
    Please help me to figure out if it's me or the question is wrong.


    Thanks a lot!

    Code:
    #include<stdio.h>#include<string.h>
    
    int main()
    {
            char * arr[] = {        "We will teach you how to",
                                    "Move a mountain",
                                    "Level a building",
                                    "Erase the past",
                                    "Make a million",
                                    "All through C!"
                            };
            char arr1[10]="mountain";
            char arr2[10]="car";
            char* found= NULL;
            int i;
    
            printf("arr1= %s will be replaced by arr2= %s in arr\n",arr1,arr2);
            for(i=0;i<6;i++)
            {
                    found = strstr(arr[i],arr1);
                    if(found)
                    {
                            strcpy(found,arr2); // **Segmentation Fault**
                    }
                    found= NULL;
           }
           return 0;
    }
    
    
    /* Another Version of that condition, char by char
                    if(found)
                    {
                            for(j=0;arr2[j]!='\0';j++)
                            {
                                    *found = arr2[j];
                                    ++found;
                            }
                            *found='\0';
                    } */
    

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Indeed, you can't modify string literals.

    Try initializing the strings dynamically inside your array, perhaps something like the following (if you are really lazy, that is :P)

    Code:
    /*************************************//**
     * Duplicate a c-string s.
     * The caller is responsible for freeing up the returned duplicate.
     *****************************************
     */
    char *s_dup( const char *src )
    {
        char   *s = NULL;
        size_t sz = 0;
    
        /* sanity check */
        if ( NULL == src ) {
            fprintf( stderr, "%s: NULL pointer argument\n", __func__ );
            return NULL;
        }
    
        sz = strlen( src ) + 1;
        if ( NULL == (s = malloc(sz)) ) {
            return NULL;
        }
    
        return (char *) memcpy( s, src, sz );
    }
    ...
    int main( void )
    {
        /* NULL terminated array of cstrings */
        char *sarr[] = {
            s_dup( "We will teach you how to" ),
            s_dup( "Move a mountain" ),
            s_dup( "Level a building" ),
            s_dup( "Erase the past" ),
            s_dup( "Make a million" ),
            s_dup( "All through C!" ),
            NULL
        };
        ...
    }
    The above is quick & dirty. In real code you should make a dedicated function for inserting dynamically one or more cstrings into your array, which will also check against malloc/calloc failure when creating the cstrings.

    Either way, remember to free all the cstrings in the array when you are done with them.
    "Talk is cheap, show me the code" - Linus Torvalds

  3. #3

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    6
    @ matticus apologies for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault when using fopen("filename","w+")
    By Marslakoo in forum C Programming
    Replies: 6
    Last Post: 11-21-2011, 08:15 AM
  2. [Segmentation Fault] fopen("filename","w")
    By gibbofresco in forum C Programming
    Replies: 7
    Last Post: 07-04-2009, 04:32 AM
  3. Replies: 46
    Last Post: 08-24-2007, 04:52 PM