Possible Duplicate:
C/C++ Char Pointer Crash
char *p = "atl";
char c;
c = ++*p; //crashing here
Why it is crashing?
I know the memory is not created for pointer increment should have be done on data.
Possible Duplicate:
C/C++ Char Pointer Crash
char *p = "atl";
char c;
c = ++*p; //crashing here
Why it is crashing?
I know the memory is not created for pointer increment should have be done on data.
p points to const data which is the string literal "atl"; that means, *p cannot be changed. But you're trying to change it by writing ++*p. That is why its crashing at runtime.
In fact, most compilers would give warning when you write char *p ="atl". You should write:
const char *p ="atl";
If you write so, then the compiler would give error when you write ++*p at compilation time itself. Detection of error at compile time is better than detection of error at runtime. See the compilation error here now:
The compilation error is:
prog.cpp:7: error: increment of read-only location ‘* p’
However, if you write
char p[] = "atl";
char c = ++*p; //ok
then its correct now. Because now p is an array which is created out of the string literal "atl". It doesn't point to the string literal itself anymore. So you can change the content of the array.