For instance, if I am getting student names from a user and use cin.getline(student.name, 50); I can assign student names. I cannot explicitly assign a student name via student.name = "John Doe"; since you cannot just copy an array over, but why does this work when i use the getline function? What is the difference? Isn’t getline() collecting a character array and then copying it to studnet.name anyway?
For clarification, I’m asking why I can use cin.getline(student.name, 50) to assign a student name but not stuent.name = "John Doe" and what is the difference between the 2 methods (why the getline() works and the direct assignment does not work).
If you look at the parameter list of the istream::getline function,
istream& getline (char* s, streamsize n );, you will notice that it takes your variable,student.name, as a pointer. This allows getline to write directly to the memory location of your c-string.edit: see Praetorian’s answer in the comments for a more detailed explanation.