Site hosted by Angelfire.com: Build your free website today!



Next: Structures as Function Up: Structures in C Previous: Defining a Structure

Accessing Members of a Structure

Each member of a structure can be used just like a normal variable. However it will be named rather differently. To return to the examples above, member name of structure st_rec will behave just like a normal array of char, however we refer to it by the name


  st_rec.name
Here the dot is an operator which selects a member from a structure.

Where we have a pointer to a structure we could dereference the pointer and then use dot as a member selector but this method is a little clumsy to type. The authors of C realised that selection of members from structure pointers would be performed quite frequently, so they added a special operator to make it possible. The operator is writen as -> and is used as follows. Assume that st_ptr is a pointer to a structure of type student We would refer to the name member as


  st_ptr -> name