When you write a string literal, i.e.:
that string lives in static storage and is of type
So the string in your code is of really of type const char * and not of type char *. A decent compiler should
issue a warning if try to declare a string literal to have the type char *.
If you're using gcc, I suggest always compiling with -Wall -W, and sometimes -ansi and -pedantic may also be warranted.
You already have the solution to your problem, you want a string to be initialised and modifiable and the correct way
is to use an array:
Code:
char foo[] = "hello";
The compiler will fill in the size for you (six characters in this case).
Trying to modify string literals is a common mistake.