+ 1

What will be the output? Explain.

struct Ournode{ char x,y,z; }; main() { struct Ournode p = {'1','0','a'+2}; struct Ournode *q = &p; printf("%c,%c",*((char*)q+1),*((char*)q+2)); return 0; }

2nd Mar 2018, 11:04 PM
Diwakar
Diwakar - avatar
4 Antworten
+ 10
Oh, sorry! To start, calculate what values p has. It has '1', '0', and 'a' + 2. When you add a number to a char, you go that many values up in the ASCII chart. Two characters above 'a' is 'c'. So p = '1', '0', and 'c'. Next, *q is a pointer to p, and it's pointing to p's first byte (which contains '1'). When you add a number to a pointer, you make it point that many bytes further up. Since chars are 1 byte, *q + 1 will take you to its second value, '0'. *q + 2 goes to the third value, 'c'. So knowing all this, which two characters are being output by printf()?
2nd Mar 2018, 11:18 PM
Tamra
Tamra - avatar
+ 7
Hey, Diwakar! If you have any quiz suggestions, please submit them to the Quiz Factory. Tap the … menu in the corner to find it. Thank you!
2nd Mar 2018, 11:07 PM
Tamra
Tamra - avatar
+ 1
thank you
3rd Mar 2018, 7:55 AM
Diwakar
Diwakar - avatar
0
I know that but in the above question I need the explanation on the output. By the way thanks for your suggestion.
2nd Mar 2018, 11:09 PM
Diwakar
Diwakar - avatar