pointer


I don't know why the output is 4 2 4

because I don't understand line 7


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main(void)
{
int x = 5, y, *px, *py = &y;
px = &x;
*py = (x + *px) / 4;
px = py;
x = y * *px;
cout << x << ' ' << y << ' ' << *px * *py << endl;
	
	
}



& thanks
1
2
(x + *px) / 4;
//(5 + 5) / 4 == 10 / 4 == 2 (integer division) 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int x = 5;
int y;
int* px;
int* py = &y; // py points to y

px = &x ; // px points to x

// ========
*py = ( x + *px ) / 4;
// can be translated to
*py = (5 + 5) / 4;
*py = 10 / 4
*py = 2;

px = py; // px now contains the memory location of y (since py points to y)

x = y * *px; // times
// can be translated to
x = 2 * 2;
x = 4;

// ===== Output ==
x==4   y==2   *py * *py // times
x==4   y==2    2 * 2

// ---------
4 2 4


edit: i didn't see that you only want line 7
Last edited on
Topic archived. No new replies allowed.