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):
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.
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.
#include <iostream>
usingnamespace 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;
}
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.
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.