+ 1
Pointer
show me an example where the value of Var and the value of Var stored in the pointer is different
4 ответов
+ 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
+ 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.
+ 1
Oh ohk ty
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?