1

It can be a rookie mistake however I am not able to point out reason for this Segmentation Fault. Below is the code :

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void revString(char *str){

    int n = strlen(str);
    char temp;

    for( int i = 0 ; i < n/2 ; i ++ ){
        // swap two chars. 

        temp = str[i];
        str[i] = str[n-i-1];
        str[n-i-1] = temp ; 
    }
}


int main()
{
    char *arr[2] = {"one","two"};
    printf("%s \n",arr[0]);  
    revString(arr[0]);
    printf("%s \n",arr[0]);
    return 0;
}

After tracking the bug using GDB, it is happening at step str[i] = str[n-i-1]. This is because of accessing str[0] and updating its value. Why is it illegal operation?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Prithi
  • 57
  • 6
  • 4
    Your array contains pointers to string literals. String literals by definition are not writeable. – kaylum Mar 09 '20 at 23:48
  • 2
    Does this answer your question? [Segmentation Fault With Char Array and Pointer in C on Linux](https://stackoverflow.com/questions/1773079/segmentation-fault-with-char-array-and-pointer-in-c-on-linux) – kaylum Mar 09 '20 at 23:50
  • That makes sense. I had an intuition that this is a conceptual mistake. – Prithi Mar 10 '20 at 00:00

2 Answers2

1

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.

So change the array of pointers to first characters of string literals

char *arr[2] = {"one","two"};

to a two-dimensional character array like for example

char arr[2][4] = {"one","two"};

Pay attention to that it is better to define the function like

char * revString( char *str )
{
    for ( size_t i = 0, n = strlen( str ) ; i < n/2 ; i++ )
    {
        // swap two chars. 
        char temp = str[i];
        str[i] = str[n-i-1];
        str[n-i-1] = temp ; 
    }

    return str;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1

Things to consider:

  • What will revString do if it is passed a null pointer?
  • What if the length of str is odd?
  • What does strlen() count, full length or ‘number’ of non-null characters?
  • Perhaps the length of str should be stored in a size_t, and given a clearer name ( like len? )
  • As hinted by Kaylum in a comment, passing a pointer as a function parameter has different behavior and rules.
  • Use a debugger, step through your function and examine what it is doing.
Jordan
  • 54
  • 1
  • 11
  • 1
    Whilst useful considerations, this does not seem to be an appropriate answer as it does not directly address the OP's actual question. – kaylum Mar 10 '20 at 00:03
  • I like being useful. Although OP is different from me, I found it valuable when I was learning C to have my mentor ask those types of questions to prod me into discovering solutions. – Jordan Mar 10 '20 at 00:05
  • That's not quite the point. On Stack Overflow, answers need to be actual answers to the question. It's good to be helpful but we also need to be aware of the intent and rules of the forum, SO in this case, that we are using. – kaylum Mar 10 '20 at 00:07