+ 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.

23rd Apr 2022, 12:11 AM
Ali Saad
6 Respostas
+ 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'
23rd Apr 2022, 12:34 AM
John Wells
John Wells - avatar
+ 6
Because the data is the constant not the pointer to it. This protects both: const char * const p = "12345";
23rd Apr 2022, 12:45 AM
John Wells
John Wells - avatar
+ 4
John Wells: Why did const not protect the char* string literal?
23rd Apr 2022, 12:38 AM
William Owens
William Owens - avatar
+ 3
p holds the address of your string. You told it hold "XYXWVU" instead killing the address plus two bytes beyond.
23rd Apr 2022, 1:26 AM
John Wells
John Wells - avatar
+ 2
John Wells , Thank you for the information.
23rd Apr 2022, 12:54 AM
SoloProg
SoloProg - avatar
0
John Wells why p is trash address ?
23rd Apr 2022, 1:13 AM
Ali Saad