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

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

if he changes the line as I said, the next line *(My_St+1)='+'; will be a valid pointer operation.
but still using an array... As in sizeof(My_St) = 7, not 1.
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.
@monadic: If you really want a pointer variable, then you can do char a[] = "abcdef"; char *My_St = a;
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.

Comments

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.

3 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.