tags:

views:

49

answers:

2

Possible Duplicate:
Why does this Seg Fault?

Hello, I have a

char* str = "blah";

And I now want to change one of the characters to something else, say a number 3. I am trying to do so with:

str[2] = '3';

However I am getting a seg fault at this line of code. Any idea why?

+3  A: 

This is a duplicate question. See here.

linuxuser27
I searched probably 3 or 4 queries to no avail. Thanks for the help.
segfault
No worries. Glad it helped you.
linuxuser27
+2  A: 

That's not an array of chars. It's a pointer to char initialized with a string constant. String constants cannot be modified, but if you make it an array of chars rather than a char pointer, it will work. e.g.

char str[] = "blah";
str[2] = '3';
Ferruccio
While this was helpful in finding my way to the solution, the link to the duplicate question explained WHY I was running into the issue, and therefore I selected it as the best answer. Nonetheless, I appreciate the help, and up voted your response.
segfault