c++ - Why increment on data pointed by Pointer crashing? -
c++ - Why increment on data pointed by Pointer crashing? -
possible duplicate: c/c++ char pointer crash
char *p = "atl"; char c; c = ++*p; //crashing here why crashing?
i know memory not created pointer increment should have done on data.
p points const info string literal "atl"; means, *p cannot changed. you're trying alter writing ++*p. why crashing @ runtime.
in fact, compilers give warning when write char *p ="atl". should write:
const char *p ="atl"; if write so, compiler give error when write ++*p @ compilation time itself. detection of error @ compile time improve detection of error @ runtime. see compilation error here now:
the compilation error is:
prog.cpp:7: error: increment of read-only location ‘* p’
however, if write
char p[] = "atl"; char c = ++*p; //ok then right now. because p array created out of string literal "atl". doesn't point string literal itself anymore. can alter content of array.
c++ visual-c++
Comments
Post a Comment