-3

On line 18, I get a seg fault in the first iteration (i = 0).

#include <stdio.h>

int main(void) {
    char* str = "mono";

    int length = 0;
    int i;

    for (i = 0; ; i++) {
        if (str[i] == '\0') {
            break;
        } else {
            length++;
        }
    }
    for (i = 0; i < length / 2; i++) {
        char temp = str[length - i - 1];
        str[length - i - 1] = str[i]; // line 18
        str[i] = temp;
    }

    printf("%s", str);
    return 0;
}

I wrote this algorithm to reverse a string.

2 Answers2

7

You are modifying a string literal:

char* str = "mono";

and string literals are non-modifiable in C.

To fix your issue, use an an array initialized by the string literal:

char str[] = "mono";
ouah
  • 142,963
  • 15
  • 272
  • 331
  • I tried that before asking and I still got the same error. Let me try again. – user3200069 Jan 15 '14 at 21:20
  • Weird. It works this time. Thanks. – user3200069 Jan 15 '14 at 21:20
  • Nit: attempting to modify a string literal results in **undefined behavior**; this is not *quite* the same thing as string literals being *non-modifiable* (not all platforms will put string literals in a read-only data segment). You should definitely treat string literals *as if* they are non-modifiable, though. – John Bode Jan 15 '14 at 21:23
  • 1
    @JohnBode Also nitpicking: terminology in K&R2: *"Strings are no longer modifiable"*, terminology in C99 Rationale: *"String literals are not required to be modifiable"* and terminology"* and terminology in C89 Rationale: *"Strings literals are specified to be unmodifiable"* – ouah Jan 15 '14 at 21:28
  • @ouah: yet neither the C99 nor C2011 standards documents themselves explicitly say that (at least, not that I've been able to find); they only say that "[i]f the program attempts to modify such an array, the behavior is undefined." Both point out that writable string literals are a common extension (appendix J.5.5). – John Bode Jan 15 '14 at 21:56
  • @JohnBode Well in C99, 6.2.5.6p15, a string literal is presented and it is written *"[...] has static storage duration and has type array of char, but need not be modifiable;"* which is the same wordings as in C99 Rationale *""String literals are not required to be modifiable"*". – ouah Jan 15 '14 at 22:02
1

Runtime Error:

char* str = "mono"; // str points to an address in the code-section, which is a Read-Only section
str[1] = 'x';       // Illegal memory access violation

Compilation Error:

const char* str = "mono"; // This is a correct declaration, which will prevent the runtime error above
str[1] = 'x';             // The compiler will not allow this

All Good:

char str[] = "mono"; // str points to an address in the stack or the data-section, which are both Read-Write sections
str[1] = 'x';        // Works OK

Notes:

  1. In all cases, a string "mono" is placed in the code-section of the program.

  2. In the last example, the contents of that string are copied into the str array.

  3. In the last example, the str array is located in the stack if str is a non-static local variable, and in the data-section of the program otherwise.

barak manos
  • 29,648
  • 10
  • 62
  • 114