Skip to main content

Section 25.5 Structures and Pointers

When dealing with a pointer to a structure it can get pretty cumbersome to access a member of the structure being pointed to.
Suppose again that we have declared a structure as before:
struct student{
  char firstName[30];
  char lastName[30];
  int birthYear;
  double aveGrade;
};
Suppose furthermore that we have a pointer:
struct student * studentptr;
which points to a particular student’s record. In order to access a member (for example birthYear) of this student’s record via the pointer we first need to dereference the pointer (*studentptr) and then access the member via the direct member selection operator .:
(*studentptr).birthYear
The parentheses around (*studentptr) are important since without them the computer would attempt to execute the member selection operator first, which would make no sense, given that studentptr is not a structure but rather a pointer to a structure.
The indirect member selection operator -> combines these steps into one:
studentptr->birthYear
and therefore accomplishes the same thing as the above. Watch the video to find out more:

If you cannot see this codecast, please click here.

Aside: Video Description.