I l @ ve RuBoard |
Structures and unions use operators to identify individual members . The membership operator is used with structures and unions, and the indirect membership operator is used with pointers to structures or unions.
. is used with a structure or union name to specify a member of that structure or union. If name is the name of a structure and member is a member specified by the structure template, then name.member identifies that member of the structure. The type of name.member is the type specified for member . The membership operator can also be used in the same fashion with unions.
struct { int code; float cost; } item; item.code = 1265;
This statement assigns a value to the code member of the structure item .
-> is used with a pointer to a structure or union to identify a member of that structure or union. Suppose that ptrstr is a pointer to a structure and that member is a member specified by the structure template. Then ptrstr->member identifies that member of the pointed-to structure. The indirect membership operator can be used in the same fashion with unions.
struct { int code; float cost; } item, * ptrst; ptrst = &item; ptrst->code = 3451;
This program fragment assigns a value to the code member of item . The following three expressions are equivalent:
ptrst->code item.code (*ptrst).code
I l @ ve RuBoard |