// declare three objects (variables) of this type (product): apple, banana, and melon. structproduct { int weight; double price; } apple, banana, melon;
// struct requires either a type_name or at least one name in object_names, but not necessarily both. struct { int weight; double price; } apple, banana, melon;
// declare three objects (variables) of this type (product): apple, banana, and melon. product apple, banana, melon;
结构体数组
1 2
// because structures are types, they can also be used as the type of arrays. product banana[3];
结构体指针
1
product * p = &apple;
创建结构体指针之后,可以使用以下运算符访问其成员变量:
Operator
Expression
What is evaluated
Equivalent
dot operator (.)
a.b
Member b of object a
arrow operator (->) (dereference operator)
a->b
Member b of object pointed to by a
(*a).b
例子:
1 2 3 4 5
apple.weight; // 3 // The arrow operator (->) is a dereference operator that is used exclusively with pointers to objects that have members. This operator serves to access the member of an object directly from its address. p->weight; // 3 // equivalent to: (*p).weight; // 3