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?

3
  • 4
    Your array contains pointers to string literals. String literals by definition are not writeable. Commented Mar 9, 2020 at 23:48
  • 2
    Does this answer your question? Segmentation Fault With Char Array and Pointer in C on Linux Commented Mar 9, 2020 at 23:50
  • That makes sense. I had an intuition that this is a conceptual mistake. Commented Mar 10, 2020 at 0:00

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

-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.

3 Comments

Whilst useful considerations, this does not seem to be an appropriate answer as it does not directly address the OP's actual question.
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.