4

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?

Community
  • 1
  • 1
finiteloop
  • 4,485
  • 8
  • 39
  • 61

1 Answers1

3

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
  • 96,346
  • 38
  • 221
  • 297
  • 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. – finiteloop Sep 16 '10 at 01:15