+ 5

Why the output is 'c', can anyone say me?

const char *p="12345"; const char **q=&p; *q="abcde"; const char *s=++p; p="UVXYZ"; cout<<*++s;

7th Jun 2020, 10:47 AM
Nikhil Maroju
Nikhil Maroju - avatar
5 Respostas
+ 5
0. p holds address of constant "12345" 1. q holds address of p. So now *q = p = &("12345") 2. Change *q to hold address of constant "abcde" This also replaces p, so now p holds the same address [&("abcde")]. 3. Increment p to address of constant "bcde" 4. Assign s = p = address of constant "bcde" 5. Change p to hold address of constant "UVXYZ" 6. Increment s to address of constant "cde" 7. Output the const char pointed to by s, which is  'c'.
7th Jun 2020, 9:37 PM
Brian
Brian - avatar
+ 4
You are welcome! 🤝
8th Jun 2020, 7:03 AM
Brian
Brian - avatar
+ 3
Ok thank you Brian
8th Jun 2020, 5:37 AM
Nikhil Maroju
Nikhil Maroju - avatar
+ 2
Nikhil Maroju, Step 5 begins with both p and s holding duplicate copies of the memory address where "bcde" is located. Then p gets reassigned to hold a different address where "UVXYZ" is stored. The address stored in s does not change, and the constant that it points to does not change.
8th Jun 2020, 5:34 AM
Brian
Brian - avatar
+ 1
Hey thank you Brian , But at 5th step we are changing p to hold address of constant "UVWYZ" so 's' value should be changed right!, As s is holding same address
8th Jun 2020, 4:29 AM
Nikhil Maroju
Nikhil Maroju - avatar