Are these two data strucures the same?
Jul 4, 2012 at 12:30am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
};
int main ()
{
string mystr;
movies_t amovie; //associating amovie with movie_t
movies_t * pmovie; // declaring pmovie as a pointer associated with movies_t. (like saying int *p)
pmovie = &amovie; // what pmovie points to is stored at amovie.
cout << "Enter title: " ;
getline (cin, pmovie->title);
cout << "Enter year: " ;
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;
cout << "\nYou have entered:\n" ;
cout << pmovie->title;
cout << " (" << pmovie->year << ")\n" ;
return 0;
}
[code] #include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} amovie;
void printmovie(movies_t movie);
int main ()
{
string mystr;
cout << "enter title" << endl;
getline (cin, amovie.title);
cout << "enter year" << endl;
getline (cin,mystr);
stringstream(mystr) >> amovie.year;
cout << "You have entered this movie" << endl;
printmovie (amovie);
return 0;
}
void printmovie(movies_t movie)
{
cout << movie.title;
cout << "(" << movie.year << ")" << endl;
}
if so than what is the point of even using pointers with data structures? Or is this just another one of the tutorial's bad examples?
Jul 4, 2012 at 6:13am UTC
That's are not same because you are making global to the instance amovie in second where's in the first it's local more secure and require less memory. By using pointer you are making your code memory efficient.
Topic archived. No new replies allowed.