+ 2

Give the ouput and explain #include<iostream.h> void main() { int a =32, *ptr=&a; char ch='A' , &cho=ch; cho+=a; *ptr+=ch; cout<<a<<" "<<ch<<endl; }

18th Jul 2016, 12:47 PM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
5 ответов
+ 2
so, you have a few things going on here. let's break it down step by step. first, you declare int a=32; this will set the value of a to 32. then you int pointer ptr gets set to the memory address of a. so be ptr points back to a (you'll see later) then you declare char ch='A', storing A in ch. then this line of code is a rather interesting one: &cho=ch; essentially what you've done here is bound the variable cho to ch. so they share the same memory location. So rather than cho storing the value of ch's memory address, it stores whatever ch stores and vice versa. so to recap we have a, which is 32, ptr, which points back to a. ch which stores 'A', and cho which is referencing ch's value.
18th Jul 2016, 5:27 PM
destro
+ 4
now for the arithmetic : cho+=a takes the value of cho, a char, and adds a to it, an int. since 'A'=65 in ASCII, 65+32=97=a;. so when you call ch in your print statement, it takes the value of cho since they share the address now, and prints out the ASCII value of 97. now, *ptr+=ch; simply says point to the address stored in ptr, which is int a, and add the integer version of ch to it, now 97 since you've changed it prior. so 97+32 =129. so that's the reason why your output it 129 a
18th Jul 2016, 5:32 PM
destro
+ 3
sorry about splitting the response, would've been to big
18th Jul 2016, 5:33 PM
destro
+ 1
i don't indersen plz complit thid
6th Aug 2016, 4:56 PM
imane
imane - avatar
0
destro Thanks a lot! 🙏😊
14th Jan 2021, 4:25 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar