Mega Code Archive

 
Categories / C++ Tutorial / Pointer
 

Use reference type eliminates the more confusing pointer notation

#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 }