Advertisement
filomancio

Structures and Pointers

Mar 2nd, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include "iostream"
  2. using namespace std;
  3.  
  4. struct Point
  5. {
  6.     double x,y;
  7. };
  8.  
  9. void DoublePoint(Point* p);
  10. void Print(Point p);
  11.  
  12. int main()
  13. {
  14.     Point p1,p2;
  15.     p1.x=9.78;
  16.     p1.y=10.98;
  17.     p2.x=99.01;
  18.     p2.y=22.23;
  19.     Print(p1);
  20.     Print(p2);
  21.     DoublePoint(&p1); //You pass the position of the variable so you have to type &var
  22.     DoublePoint(&p2);
  23.     Print(p1);
  24.     Print(p2);
  25.     system("PAUSE");
  26. }
  27.  
  28. void DoublePoint(Point* p)
  29. {
  30.     p->x=2*p->x;  //With Pointers you have to use "->" instead of "." and you must not type "*" before the variable because
  31.     p->y=2*p->y;  //the program is using structures
  32.     return;
  33. }
  34.  
  35. void Print(Point p)
  36. {
  37.     cout<<p.x<<' '<<p.y<<endl;
  38.     return;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement