Please explain this. TOO MANY REFERENCES

Please explain this line by line. The user enters -2 and -5 the input respectively:

#include <iostream>
using namespace std;
int main( )
{
int x,y,*u,*v;
/* Point 1 */
cout<<"Enter two integers> ";
cin>>x>>y;
u = &y;
v = u;
/* Point 2 */
*v++;
v=&x;
/* Point 3*/
cout<<"x = "<<x<<" y = "<<y<<" u = "<<*u<<" v = "<<*v<<endl;
x++;
u = v;
/* Point 4 */
u = &y;
v = &x;
*u = *v;
/* Point 5 */
y = *v;
*u = x;
/* Point 6 */
cout<<"x = "<<x<<" y = "<<y<<" u = "<<*u<<" v = "<<*v<<endl;
return 0;
}


Output (proof that I dont want you to write this program for me):


Enter two integers> x = -2 y = -4
x = -1 y = -1
Last edited on
This smells like pointer homework
This smells like very badly formatted code. I can't even begin to read that.
its not homework we were given last years practice exam to study for the final exam and I am trying to find how they got the output. My final is tomorrow. Please help I am sincerely in need of help. And we are not concerned about the entire program just what would run so far
JCharles is it better to get help or you figure out how the program works? I think its better if you figure out how program the works. If you have a exam tomorrow it is better for you as you will have a great understanding while going into the exam. Use your head and you will get far. Good luck on the exam.
no i have been helped here before and was able to put things together when people did help
Try going through each line of the program and commenting it. Then analyze your results, and see if what you were thinking is correct, then post here with that.
thank you zhuge i can do that

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
#include <iostream>
using namespace std;
int main( )
{
int x,y,*u,*v;
/* Point 1 */
cout<<"Enter two integers> ";
cin>>x>>y;
u = &y; // u is -5
v = u; // v is u which is -5
/* Point 2 */
*v++; // v is -4
v=&x; // v is -2
/* Point 3*/
cout<<"x = "<<x<<" y = "<<y<<" u = "<<*u<<" v = "<<*v<<endl; // x = -2  y= -4 u= -2 v = -2  
x++; // x = -1
u = v;
/* Point 4 */
u = &y; // u is -4 ???
v = &x; // v is -4 because u = v ???
*u = *v; //???
/* Point 5 */
y = *v; // -4 ???
*u = x; // -1 ???
/* Point 6 */
cout<<"x = "<<x<<" y = "<<y<<" u = "<<*u<<" v = "<<*v<<endl; x= -1 y= -4  u = -1  v= -4   ???
return 0;
}



So what is going on now
Seeing people's typical response of "BHUH, JUZ FIGUR IT OUT URSELF! ULL B BETTAR" pisses me off, so I'll do the best I can to explain what I can tell.

btw, why in the hell did your prof even come up with this? This is pointer testing overkill, but then again most profs that teach programming are fantastic at coming up with needlessly confusing tests and poorly covering material.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int x,y,*u,*v;  // *u and *v are pointers which each will hold a memory address.
/* Point 1 */
cout<<"Enter two integers> ";
cin>>x>>y; //x and y get input by the user
u = &y; // u is now set to the memory address of y (indicated by &y)
v = u; // v is set to u, so now v and u hold the memory address of y
/* Point 2 */
*v++; // This moves the address of the pointer by the size of what kind of pointer it is.
//Even if you don't know what that means it doesn't really matter here because of what happens next
v=&x; // Now v holds the address of x
/* Point 3*/
cout<<"x = "<<x<<" y = "<<y<<" u = "<<*u<<" v = "<<*v<<endl; // print out x, y, what u holds (y), and what v holds (x)
x++; // increment x by one.
u = v; // the pointer u now holds what v holds
/* Point 4 */
u = &y; // the pointer u now holds the memory address of y
v = &x; // v now holds the address of x
*u = *v; // what u holds is now equal to what v holds
/* Point 5 */
y = *v; // y is set to what v holds, but it doesn't matter. They're already the same.
*u = x; // u now holds the address of x... which is pointless because they're still the same.
/* Point 6 */
cout<<"x = "<<x<<" y = "<<y<<" u = "<<*u<<" v = "<<*v<<endl; // prints out x, y, what u holds(x), and what v holds(y) and they're all the same number because of what happened earlier 


I'm not 100% sure I've got it right, but I did what I could. I hope this helps.
Last edited on
Topic archived. No new replies allowed.