0

why this output comes 4 and why not -4; #include<iostream> using namespace std; int main() { int a=4; int *p=&a; p-= 8; cout<<a; }

31st Aug 2016, 7:16 PM
Lekhraj Singh
Lekhraj Singh - avatar
3 ответов
+ 3
p-=8 changes the address of the pointer, not the value of a. To get -4, replace this line with: *p -= 8;
31st Aug 2016, 7:51 PM
Zen
Zen - avatar
+ 1
"P" is a pointer which contains adderss of variable not value of it! By writing "p-=8" you dont even change the address because if address was changed another value would be resualted in output you can simply write a-=8 or *p-=8
31st Aug 2016, 8:54 PM
Fardad Fateh
+ 1
* means value of to get -4 try *p-=8; //value of p=p-8;
1st Sep 2016, 12:48 PM
Prateek Singh
Prateek Singh - avatar