Accessing structs and unions via pointers

When accessing a field of a struct or a union, we have used the dot operator followed by the field's name. When using a pointer, rather than a variable there are two ways of achieving this effect.

Firstly, the dereferencing operator, *, can be used as before and the dot access applied to the outcome. As dot has a higher precedence than *, parentheses must be used, as follows.

      (*ptr).fred
A more convenient notation is supplied by the indirect member access operator, ->. The following is equialent to the first example:
      prt->fred

Exercises on this section.


Next - Building a linked list in C.

Back to Contents page.