Changing individual characters of a character array in C

Possible duplicate:
Why is this Seg Fault?

Hello i have

char* str = "blah"; 

And now I want to change one of the characters to something else, for example, to number 3. I'm trying to do this with

 str[2] = '3'; 

However, I get a seg error in this line of code. Any idea why?

+4
source share
1 answer

This is not an array of characters. This is a pointer to a char initialized by a string constant. String constants cannot be changed, but if you create an array of characters, not a char pointer, it will work. eg.

 char str[] = "blah"; str[2] = '3'; 
+3
source

All Articles