Sams Teach Yourself C in 21 Days

(singke) #1
(*p_part).number = 100;
*p_partmust be enclosed in parentheses because the structure member operator (.) has
a higher precedence than the indirection operator (*).
A second way to access structure members using a pointer to the structure is to use)>)>
(indirect membership) operator> the indirect membership operator,which consists of the
characters->(a hyphen followed by the greater-than symbol). (Note that when they are
used together in this way, C treats them as a single operator, not two.) This symbol is
placed between the pointer name and the member name. For example, to access the num-
bermember of gizmowith the p_partpointer, you would write
p_part->number
Looking at another example, if stris a structure,p_stris a pointer to str, andmembis a
member of str, you can access str.membby writing
p_str->memb
Therefore, there are three ways to access a structure member:


  • Using the structure name

  • Using a pointer to the structure with the indirection operator (*)

  • Using a pointer to the structure with the indirect membership operator (->)
    Ifp_stris a pointer to the structure str, the following three expressions are all equiva-
    lent:
    str.memb
    (*p_str).memb
    p_str->memb


270 Day 11

Some people refer to the indirect membership operator as the “structure
Note pointer operator.”

Working with Pointers and Arrays of Structures ..........................................

You’ve seen that arrays of structures can be a very powerful programming tool, as can
pointers to structures. You can combine the two, using pointers to access structures that
are array elements.

18 448201x-CH11 8/13/02 11:17 AM Page 270

Free download pdf