10.2.4 Accessing a Nested Structure
To access the member c, which is a structure, use the expression px->c. Its R‐value is the entire structure.
为了访问本身也是结构的成员。我们可以使用表达式px->c 。它的左值是整个结构。
The dot operator can be added to this expression to access specific members of c. For example, the expression px->c.a has the following R‐value:
这个表达式可以使用点操作符访问c 的特定成员。例如,表达式px->c.a 具有下面的右值:
This expression contains both the dot and arrow operators. The arrow is used because px is not a structure, it points to a structure. Then the dot operator is used because px->c does not point to a structure, it is a structure.
这个表达式既包含了点操作符,也包含了箭头操作符。之所以使用箭头操作符,是因为px 并不是一个结构,而是一个指向结构的指针。接下来之所以要使用点操作符是因为px->c 的结果并不是一个指针,而是一个结构。
Here is a more complex expression:
这里有一个更为复杂的表达式:
*px->c.b
Examining this expression is easy if you take one step at a time. There are three operators, and the arrow goes first px->c gives the structure c. Adding .b to the expression selects the member b from structure c. b is an array, so px->c.b is a(constant) pointer to the first element of the array. Finally, the indirection is applied to this pointer, so the result is the first element of the array. The expression is diagrammed below.
如果你逐步对它进行分析,这个表达式还是比较容易弄懂的。它有三个操作符,首先执行的是箭头操作符。px->c 的结果是结构c 。在表达式中增加.b 访问结构c 的成员b 。 b 是一个数组,所以px->b.c 的结果是一个(常量〉指针,它指向数组的第1 个元素。最后对这个指针执行间接访问,所以表达式的最终结果是数组的第1 个元素。这个表达式可以图解如下:
上一章 Pointers on C——10 Structures and Unions.8