+ 3
I NEED EXPLAIN!
const char *p="12345"; const char **q=&p; *q="abcde"; const char *s=++p; p="XYZWVU"; cout<<*++s; THE OUTPUT:c.
6 Answers
+ 8
const char *p="12345"; // same as const char p[]="12345"
const char **q=&p; // pointer to p[]
*q="abcde"; // p[] = "abcde"
const char *s=++p; // p = &p[1]; s == &p[1]
p="XYZWVU"; // trash address at p
cout<<*++s; // s == &p[2]; output p[2] or 'c'
+ 6
Because the data is the constant not the pointer to it. This protects both:
const char * const p = "12345";
+ 4
John Wells: Why did const not protect the char* string literal?
+ 3
p holds the address of your string. You told it hold "XYXWVU" instead killing the address plus two bytes beyond.
+ 2
John Wells ,
Thank you for the information.
0
John Wells why p is trash address ?