Use reference type eliminates the more confusing pointer notation. : pointer reference « Pointer « C++ Tutorial






#include <iostream>
using namespace std;

struct stStudent {
 char   pszName[66],
        pszAddress[66],
        pszCity[26],
        pszState[3],
        pszPhone[13];
 int    icourses;
 float  GPA;
};

void vByValueCall     (stStudent   stAStudent);
void vByVariableCall  (stStudent *pstAStudent);
void vByReferenceCall (stStudent &rstAStudent);

int main(void)
{
 stStudent astLargeClass[100];
  
 astLargeClass[0].icourses = 10;

 vByValueCall( astLargeClass[0]);
 cout << astLargeClass[0].icourses << "\n"; // icourses still 10

 vByVariableCall(&astLargeClass[0]);
 cout << astLargeClass[0].icourses << "\n"; // icourses = 20

 vByReferenceCall( astLargeClass[0]);
 cout << astLargeClass[0].icourses << "\n"; // icourses = 30
}
  
  void vByValueCall(stStudent  stAStudent)
{
 stAStudent.icourses += 10;    // normal structure syntax
}

void vByVariableCall(stStudent *pstAStudent)
{
 pstAStudent->icourses += 10;  // pointer syntax
}

void vByReferenceCall(stStudent &rstAStudent)
{
 rstAStudent.icourses += 10;   // simplified reference syntax
}








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