Possible Duplicate:
Why does C have a distinction between -> and . ?
Lets say that I have this structure:
struct movies
{
string title;
int year;
} my_movie, *ptrMovie;
Now I access my_movie like this: my_movie.year = 1999;
Now to access a pointer I must do this: ptrMovie->year = 1999;
Why do pointers use the -> operator and normal data types use the . operator? Is there any reason they couldn’t both use the . operator?
The . operator accesses a member of a structure and can operate only on structure variables.
If you want to do this to a pointer, you first need to dereference the pointer (using
*) and then access the member (using.). Something likeThe
->operator is a shorthand for this.