I decided to retry to learn C++ by my own and now I have a question regarding pointers. I understand what pointers do, but can somebody explain the difference between these two pieces if code?
I have a structure student
:
struct student {
char* name;
int age;
int grade;
};
and two ways of outputting it's data:
void printData(student p) {
cout << p.name << endl;
cout << p.age << endl;
cout << p.grade << endl;
}
int main() {
student ST = { "Dave",20,81 };
printData(ST);
return 0;
}
and:
void printData(student* p) {
cout << p->name << endl;
cout << p->age << endl;
cout << p->grade << endl;
}
int main() {
student ST = { "Dave",20,81 };
printData(&ST);
return 0;
}
The question is: why should I stick with pointers if I could just pass a student object to the printData
function?