+ 1

C++ Pointers

So in the code below, if pointer pa was equal to pointer pa plus pointet pb would this change the value of variable a with it (since *pa is equal to var a)or it would it just change the pointer pa only? Code: #include <iostream> using namespace std; int main() { int a = 12; int b = 13; int * pa = &a; int * pb = &b; *pa = *pa + *pb; return 0; }

30th Dec 2017, 3:21 AM
Khanh Dao
Khanh Dao - avatar
2 Respostas
+ 6
No it would also change the value of a The pointer changes the value of where a is saved :)
30th Dec 2017, 3:37 AM
Dapper Mink
Dapper Mink - avatar
+ 8
A pointer points to the value of the address it stores. Pointer pa points to the value in which the address belongs to variable a. Altering the value pointed by pointer pa is equivalent to altering the value of variable a, because the referenced memory address is the same.
30th Dec 2017, 3:41 AM
Hatsy Rei
Hatsy Rei - avatar