Search code examples
cpointersreversec-stringsfunction-definition

;It shows runtime when i am using an array of pointers to strings,or is it because i cant pass data to a function from array of pointers


    #include <stdio.h>
    #include <string.h>
    //reversal function

    void reverseString(char* str)
    {
        int l, i;
        char *begin_ptr, *end_ptr, ch;
        l = strlen(str);
        begin_ptr = str;
        end_ptr = str;

        //move  the ptr  to the final pos
        for (i = 0; i < l - 1; i++)
            end_ptr++;
        //pointer swaping
        for (i = 0; i < l / 2; i++)
        {
            ch = *end_ptr;
            *end_ptr = *begin_ptr;
            *begin_ptr = ch;
            begin_ptr++;
            end_ptr--;
        }
    }

    // Driver code

---------------------------------main--------------------------------------------------------------------------------------------------- the function call sends the address of the first string in the array

int main()
{
    char *str[ ] = {"To err is human...","But to really mess things up...","One needs to know C!!"};
    for(int i=0;i<3;i++)
    {
    reverseString(str[i]);  //funtion call
    printf("Reverse of the string: %s\n", str[i]);
    }

    return 0;
}

Solution

  • You may not modify a string literal. Any attempt to modify a string literal results in undefined behavior.

    From the C Standard (6.4.5 String literals)

    7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

    You should declare a to-dimensional array like

    enum { N = 32 };
    char str[][N] = 
    {
        "To err is human...",
        "But to really mess things up...",
        "One needs to know C!!"
    };
    

    Pay attention to that the function revreseString is too complicated. Also it is better when the function returns pointer to the reversed string. The function can be defined the following way using pointers

    char * reverseString( char *s )
    {
        if ( *s )
        {
            for ( char *p = s, *q = s + strlen( s ); p < --q; ++p )
            {
                char c = *p;
                *p = *q;
                *q = c;
            }
        }
    
        return s;
    }
    

    Here is a demonstrative program

    #include <stdio.h>
    #include <string.h>
    
    char * reverseString( char *s )
    {
        if ( *s )
        {
            for ( char *p = s, *q = s + strlen( s ); p < --q; ++p )
            {
                char c = *p;
                *p = *q;
                *q = c;
            }
        }
    
        return s;
    }
    
    int main(void) 
    {
        enum { N = 32 };
        char s[][N] = 
        {
            "To err is human...",
            "But to really mess things up...",
            "One needs to know C!!"
        };
    
        for ( size_t i = 0; i < sizeof( s ) / sizeof( *s ); i++ )
        {
            puts( reverseString( s[i] ) );
        }
    
        return 0;
    }
    

    The program output is

    ...namuh si rre oT
    ...pu sgniht ssem yllaer ot tuB
    !!C wonk ot sdeen enO