+ 1

Pointer

show me an example where the value of Var and the value of Var stored in the pointer is different

15th Apr 2017, 11:22 AM
Shreya Parmar
Shreya Parmar - avatar
4 Respostas
+ 1
@enes guney in your example the first output is 10 and the last one too is 10 right... where the first output the value of Var and the last output the content of the pointer right so i want the code where the output for both of these is a different value
15th Apr 2017, 11:41 AM
Shreya Parmar
Shreya Parmar - avatar
+ 1
Oh, now I understand you. A pointer data-type is always pointing to other existing data-types like int, str, a class, whatever. So what does a pointer do? It contains the address of the data (in our case int variable a) so it points to it. When I pointer points to some thing it's same as the data it points to, the only difference is that the variable contains the value and the pointer contains the address of the value. So if you change the value via the pointer or the variable the value still stays the same for both the pointer and the variable. | 10 | (address 1000) = a | 1000 | (address 2000) = p you see, a contains the value directly and p the address of it. so it doesnt matter if you change the value, its still the same value for both.
15th Apr 2017, 12:05 PM
EagleEye
EagleEye - avatar
+ 1
Oh ohk ty
15th Apr 2017, 12:07 PM
Shreya Parmar
Shreya Parmar - avatar
0
int a = 10; int *p = &a; cout << a << endl; // output: 10 cout << p << endl; // output: 0xffffff ( address of a in main memory) cout << *p << endl; // output: 10 U understand it now?
15th Apr 2017, 11:34 AM
EagleEye
EagleEye - avatar