char *

char *s = "abcd";
creates a char pointer that points to beginning of "abcd".

why does my program crash when I do this
*s = 'z';

I suspect it crashes because I try to access some part of memory I am not supposed to, but I dont understand it.
String literals are constant character arrays.
As such, you cannot assign their address to a char* pointer. The line you posted should not compile. If it does, get a new compiler, yours is probably ancient.

const char* s = "abcd";
would be correct.

String literals are usually in read-only parts of memory, so that explains why in your case the program still crashes on the second line, even though the compiler let the first one pass (wrongly).
You can however use string literals to initialize an array: char s[] = "abcd"; in this way, you can modify them
you might using visual studio, in visual studio char * s = " " is consider as a const string

& trying to change it gives runtime error , ( i think it is access violation 0x0000005 i am not

sure )
try
char arr[] = "";

hope it helps
Declaring the following and then changing it is bad practace. If you copy a string more than 4 characters into s you will corrupt the stack.

 
char s[] = "abcd";


You should use either CString (if in VS) or STL string class.
Topic archived. No new replies allowed.