Structure and Union Operators

I l @ ve RuBoard

Structure and Union Operators

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.

The Membership Operator

. 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.

Example

 struct {         int code;         float cost; } item; item.code = 1265; 

This statement assigns a value to the code member of the structure item .

The Indirect Membership Operator (or Structure Pointer Operator)

-> 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.

Example

 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


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net