Advertisement
TheWhiteFang

pass by reference x pass by address

Dec 18th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void AddOne(int &input) //passed by reference
  6. {
  7.     input++;
  8. }
  9.  
  10. int main3()
  11. {
  12.     int x = 5;
  13.     cout << "x = " << x << endl;
  14.     AddOne(x);
  15.     cout << "x = " << x << endl; //value of x is incremented to 6
  16.     return 0;
  17. }
  18.  
  19. //void chg(int *pValue)
  20. //{
  21. //  *pValue = 6; //dereferenced pointer
  22. //}
  23. //int main()
  24. //{
  25. //  int nValue = 5;
  26. //  cout << "nValue = " << nValue << endl;
  27. //  chg(&nValue); //pointer takes in address of nValue
  28. //  cout << "nValue = " << nValue << endl; //nValue is changed to 6
  29. //  return 0;
  30. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement