pointer to a string

char *p="Hello",*l="cello";
*p=*k;//or p[0]='c';it crashes again
cout<<p;
the program rashes why??
I have some idea but not very clear about it...
some one please explain the concept
sorry understood my mistake:
p is a pointer defined to point to a string constant we can always make it to point to another string constant but if we try to change the individual elements result is undefined..
Last edited on
String literals ("hello" in this case) are not of type char*, but const char*. Line p = "Hello" doesn't allocate anything, it just makes p point to a string already somewhere in the memory. If you want to modify it, copy it either using new and strcpy or simply an std::string.
Topic archived. No new replies allowed.