Recently I've been learning C++ after only using web programming so far things have been going quite well working through the cplusplus tutorials. One thing I'm struggling to get my head around though is the use of pointers referencing objects in a data structure. Basically:
string mystr;
movies_t amovie; // create new object amovie from structure movies_t
movies_t* pmovie; // create a new pointer with type movies_t
pmovie = &amovie; // reference address of new object into pointer
cout << "Enter movie title: ";
getline(cin, pmovie->title);
cout << "Enter year: ";
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;
cout << endl << "You have entered:" << endl;
cout << pmovie->title;
cout << " (" << pmovie->year << ")" << endl;
Can be written just as easily as:
string mystr;
movies_t amovie;
cout << "Enter movie title: ";
getline(cin, amovie.title);
cout << "Enter year: ";
getline(cin, mystr);
(stringstream) mystr >> amovie.year;
cout << endl << "You have entered:" << endl;
cout << amovie.title;
cout << " (" << amovie.year << ")" << endl;
I understand their use in arrays, but am struggling to grasp why using pointers would be preferable than referencing the values themselves from a structure.