Pointers on C——10 Structures and Unions.7

10.2.1 Accessing the Pointer


Letʹs begin with the pointer variable. The R‐value of the expression px is:

让我们从指针变量开始。表达式px 的右值是:


Pointers on C——10 Structures and Unions.7_第1张图片

px is a pointer variable but there isnʹt any indirection operator, so the value of the expression is the contents of px. The L‐value of this expression is:

px 是个指针变量,但此处并不存在任何间接访问操作符,所以这个表达式的值就是px 的内容。这个表达式的左值是:


Pointers on C——10 Structures and Unions.7_第2张图片

which shows that the old value of px is about to be replaced by a new value.

它显示了px 的旧值将被个新值所取代。


Now consider the expression px + l. This expression is not a legal L‐value because its value is not stored in any identifiable memory location. The expressionʹs R‐value is more interesting. If px had been pointing to an element of an array of structures, this expression would point to the next structure in the array. As it is, the expression is illegal because there is no way of telling whether what comes next in memory is one of these structures or something else. The compiler is not able to detect such errors, so it is up to you to determine when pointer arithmetic is meaningful.

现在考虑表达式px + 1 。这个表达式并不是个合法的左值,因为它的值并不存储于任何可标识的内存位置。这个表达式的右值更为有趣。如果px 指向个结构数组的元素,这个表达式将指向该数组的下个结构。但就算如此,这个表达式仍然是非法的,因为我们没办法分辨内存下个位置所存储的是这些结构元素之还是其他东西。编译器无法检测到这类错误,所以你必须自己判断指针运算是否有意义。


10.2.2 Accessing the Structure

We can apply indirection to the pointer with the * operator. The R‐value of the expression *px is the entire structure to which px points.

我们可以使用* 操作符对指针执行间接访问。表达式*px 的右值是px 所指向的整个结构


Pointers on C——10 Structures and Unions.7_第3张图片

The indirection fallows the arrow, which is shown as a solid line, and the result is the whole structure. You can assign this expression to another structure of the same type,or you can use it as the left operand of the dot operator to select a specific member. You can also pass it as an argument to a function or return it as the value of a function (though there are some efficiency concerns about these last two operations that will be discussed later). The L‐value of the expression *px is:

间接访问操作随箭头访问结构,所以使用实线显示,其结果就是整个结构。你可以把这个表达式赋值给另个类型相同的结构,你也可以把它作为点操作符的左操作数,访个指定的成员你也可以把它作为参数传递给函数,也可以把它作为函数的返回值返回〈不过,关于最后两个操作,需要考虑效率问题,对此以后将会详述〉。表达式*px 的左值是:


Pointers on C——10 Structures and Unions.7_第4张图片

Here, the structure is about to receive a new value, of more precisely, new values for all of its members. As an L‐value, it is the place that is important, not the values contained in the place.

这里,结构将接受个新值,或者更精确地说,它将接受它的所有成员的新值作为值,重要的是位置,而不是这个位置所保存的值。


The expression *px + 1 is illegal, because the result of *px is a structure. Addition is not defined between structures and integers. But what about the expression*( px + 1 ) ? If x had been an element of an array, this expression would refer to the structure that followed it. But x is a scalar, so this expression is actually illegal.

表达式*px + 1 是非法的,因为*px 的结果是个结构C 语言并没有定义结构和整型值之间的加法运算。但表达式*(px + 1) 又如何呢?如果x 是个数组的元素,这个表达式表示它后面的那个结构。但是, x 是个标量,所以这个表达式实际上是非法的

上一章 Pointers on C——10 Structures and Unions.6

你可能感兴趣的:(Pointers,on,C)