Use a reference parameter. : pointer reference « Pointer « C++ Tutorial






#include <iostream>
using namespace std;
   
void neg(int &i); // i now a reference
   
int main()
{
  int x;
   
  x = 10;
  cout << x << " negated is ";
   
  neg(x); // no longer need the & operator
  cout << x << "\n";
   
  return 0;
}
   
void neg(int &i)
{
  i = -i; // i is now a reference, don't need *
}








11.8.pointer reference
11.8.1.Use reference type eliminates the more confusing pointer notation.
11.8.2.Use a reference parameter.
11.8.3.uses reference parameters to swap the values of the variables it is called with