+ 5
C++ output - Who does explain me?
Why final values of b is 300, whilst a remains 200? It's like p=q assignment is left-to-right! #include <iostream> using namespace std; int main() { int a = 200, b = 100; int *p = &a, *q = &b; p = q; *p = 300; cout << b << a; return 0; }
3 Antworten
+ 5
Because of line
p = q
p points to b too. That means that *p = 300 alters b, not a.
+ 4
Tricky! Just take a breath and read the code..
*q=&b // pointer q is initialized with &b
p=q // the address value in q(which is &b) is assigned to p
Now p contains &b
*p =300 // 300 is assigned to b trough p pointer.
+ 4
When p=q was coded, p started pointing to b and then value was substituted in case of p. So tht means we changed b's value.