3

im having troubles with this code

int main() {
     char *My_St = "abcdef";
     *(My_St+1)='+';
     printf("%s\n",My_St);
     return 0;
}

i built this code and has no errors, but when i try to run it, it throws a segmentation fault, could someone tell what's wrong

jack
  • 31
  • 1
  • 2

4 Answers4

6

You can't because you are trying to modify const data.

change it to:

char My_St[] = "abcdef";

Then you will be able to change it.

Think about what you were doing, you were declaring a pointer that pointed to "abcdef". It IS a pointer, not an array of chars. "abcdef" lives in the farm, I mean, in the .text area of your program and that is immutable.

When you do it the way I've shown, you are telling the compiler: i'm declaring this array, that will have as many chars as are needed to accommodate "abcdef" and also, as you are there, copy "abcdef" to it.

Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43
  • if he changes the line as I said, the next line `*(My_St+1)='+';` will be a valid pointer operation. – Vinicius Kamakura Jul 13 '11 at 23:15
  • but still using an array... As in sizeof(My_St) = 7, not 1. – alternative Jul 13 '11 at 23:17
  • If you really want to be that picky, he said he wants to **change** the array using pointers, nothing more. `*(My_St+1)='+'` is using pointers. I told him to change ONE line of his code. In your answer you are not CHANING anything. Jesus. – Vinicius Kamakura Jul 13 '11 at 23:21
  • @monadic: If you really want a pointer variable, then you can do `char a[] = "abcdef"; char *My_St = a;` – caf Jul 14 '11 at 00:26
3

You provided a hint to the compiler by declaring My_St with type char *. Assigning a string literal to this pointer essentially makes it a const char * because a string literal cannot be modified, meaning the memory location is read-only. Writing to that read-only memory location is what is producing your segfault. Change it from char *My_St to char My_St[] to get it working.

2

char *My_St refers to constant memory, most likely. You will need to dynamically allocate your string and then fill it (using strcpy).

char *str = malloc(7);
strcpy(str, "abcdef");

Or

char *str = strdup("abcdef");

And then it is safe to modify str.

alternative
  • 12,703
  • 5
  • 41
  • 41
  • I actually get no segfault when running it but it prints abcdef, and yes you're right my_st is in the .data section of the program because its a compile time constant – Jesus Ramos Jul 13 '11 at 23:10
  • @Jesus I don't think the specification says that it has to be in constant memory, just that it should be a `const char *`. So its really indeterminate behavior to modify a string literal. – alternative Jul 13 '11 at 23:16
  • gcc places it in the .data section because placing such things on the stack is impractical, also the segfault is probably caused from a protected memory access (.data section is protected) – Jesus Ramos Jul 13 '11 at 23:18
0

The basics are correct, however your character string is (behind the scenes) constant and can't be modified. You'd have to define a array of chars (e.g. char[20]), copy the string into it and then modify the character.

To be 100% correct you'd have to write const char *My_St = "abcdef"; which makes it clearer that you can't do what you're trying to do.

Mario
  • 35,726
  • 5
  • 62
  • 78