Suri

Suri

  • NA
  • 113
  • 11.2k

Why it's giving output 3 while at same location I'm getting 5

Jul 1 2021 11:25 AM

const int a = 3;          
const int *ptr_to_a = &a; 
int *ptr;
ptr = (int*)ptr_to_a;
cout<<(*ptr_to_a)<<" "<<ptr_to_a<<endl;;
cout<<(a)<<" "<<&a<<endl;;
cout<<*ptr<<" "<<ptr<<endl;


// changing here. 
(*ptr) = 5;  
cout<<(*ptr_to_a)<<" "<<ptr_to_a<<endl;;
cout<<(a)<<" "<<&a<<endl;;
cout<<*ptr<<" "<<ptr<<endl;


Output is 

3 0x7ffe87e48554
3 0x7ffe87e48554
3 0x7ffe87e48554
5 0x7ffe87e48554
3 0x7ffe87e48554
5 0x7ffe87e48554

Can anyone explain why its giving  output 3 while at same location im getting value 5.


Answers (1)