HELP QUICK

Could someone explain to me what is going 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
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>
2. using namespace std;
3.
4.
5. void magic(int &X, int Y)
6. {
7. int temp = Y;
8. Y = X+1;
9. X = temp+3;
10. cout << "X, Y: " << X << " " << Y << endl;
11.}
12.void voodoo(int* X, int* &Y)
13.{
14. int temp = *Y;
15. Y = X;
16. *X = temp+1;
17. *Y = temp;
18. cout << "X, Y: " << *X << " " << *Y << endl;
19.}
20.int main()
21.{
22. int x, y, a, b;
23. int *px, *py;
24. x = 4;
25. y = 3;
26. a = 2;
27. b = 1;
28.
29. px = &x;
30. py = &y;
31. cout << "x, y, a, b: " << x << " " << y << " " << a << "
" << b << endl;
32. magic(a, b);
33. cout << "x, y, a, b: " << x << " " << y << " " << a << "
" << b << endl;
34. voodoo(px, py);
35. cout << "x, y, a, b: " << x << " " << y << " " << a << "
" << b << endl;
36.! !
37.}


I understood up to line 34, then got lost :(
Last edited on
closed account (ypLhURfi)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 #include <iostream>
using namespace std;


 void magic(int &X, int Y)
 {
 int temp = Y;
Y = X+1;
X = temp+3;
cout << "X, Y: " << X << " " << Y << endl;
}
 void voodoo(int* X, int* &Y)
{
 int temp = *Y;
 Y = X;
 *X = temp+1;
*Y = temp;
 cout << "X, Y: " << *X << " " << *Y << endl;
}
 int main()
{
 int x, y, a, b;
 int *px, *py;
 x = 4;
 y = 3;
 a = 2;
 b = 1;

 px = &x;
 py = &y;
 cout << "x, y, a, b: " << x << " " << y << " " << a 
<< " " << b << endl;
 magic(a, b);
 cout << "x, y, a, b: " << x << " " << y << " " << a 
<< " " << b << endl;
 voodoo(px, py);
 cout << "x, y, a, b: " << x << " " << y << " " << a << " "
 << b << endl;

return 0;
}
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
28
29
30
31
32
33
34
35
36
37
#include <iostream>
 using namespace std;

void magic(int &X, int Y)
{
 int temp = Y;
 Y = X+1;
 X = temp+3;
 cout << "X, Y: " << X << " " << Y << endl; //2) 4 3
}

void voodoo(int* X, int* &Y)
{
 int temp = *Y;
 Y = X;
 *X = temp+1;
 *Y = temp;
 cout << "X, Y: " << *X << " " << *Y << endl; //4) 3 3
}

int main()
{
 int x, y, a, b;
 int *px, *py;
 x = 4;
 y = 3;
 a = 2;
 b = 1;
 px = &x;
 py = &y;
 cout << "x, y, a, b: " << x << " " << y << " " << a << " " << b << endl; //1) 4 3 2 1
 magic(a, b);
 cout << "x, y, a, b: " << x << " " << y << " " << a << " " << b << endl; //3) 4 3 4 1
 voodoo(px, py);
 cout << "x, y, a, b: " << x << " " << y << " " << a << " " << b << endl; //5) 3 3 4 1

}
1
2
3
4
5
6
7
8
void voodoo(int* X, int* &Y) //px=&x=4, py=&y=3
{
 int temp = *Y; //3
 Y = X; //4
 *X = temp+1; //3+1=4
 *Y = temp; //3
 cout << "X, Y: " << *X << " " << *Y << endl; //4) 4, 3
}


This is what I am understanding..what am I doing wrong?
Y = X; //4 no.

it means Y left its previous address.

now, Y is pointing to where X points.
so, if you write *X=5; then *Y will be 5 too.

similarly, *Y=3 // so *X = 3
1
2
3
4
5
6
7
8
void voodoo(int* X, int* &Y) //X=px, so X->x  Y is a synonym for py. Both point to y
{
 int temp = *Y;     // temp = 3
 Y = X;             // Y and py point to x
 *X = temp+1;       // x = 3+1
 *Y = temp;         // x = 3
 cout << "X, Y: " << *X << " " << *Y << endl; //4) 3, 3
}
Topic archived. No new replies allowed.