Pointers on C——6 Pointers.15

6.14 Summary


Each location in the computerʹs memory is identified by an address. Adjacent locations are often grouped together to allow larger ranges of values to be stored. A pointer is a value that represents a memory address.

计算机内存中的每个位置都由一个地址标识。通常,邻近的内存位置合成一组,这样就允许存储更大范围的值。指针就是它的值表示内存地址的变量。


Neither you nor the computer can determine the type of a value by examining its bits; the type is implicit in how the value is used. The compiler helps us by ensuring that values are used in a way that is appropriate to how they are declared.

无论是程序员还是计算机都无法通过值的位模式来判断它的类型。类型是通过值的使用方法隐式地确定的。编译器能够保证值的声明和值的使用之间的关系是适当的,从而帮助我们确定值的类型。


The value of a pointer variable is not the value to which it points. Indirection must be applied to a pointer to obtain the value that it points to. The result of applying indirection to a ʺpointer to integerʺ is an integer.

指针变量的值并非它所指向的内存位置所存储的值。我们必须使用间接访问来获得它所指向位置存储的值。对一个"指向整型的指针"施加间接访问操作的结果将是一个整型值。


Declaring pointer variable does not automatically allocate any memory in which to store values. Before indirection can be performed on the pointer, it must be initialized point either to existing memory or to dynamically allocated memory. Indirection on an uninitialized pointer variable is illegal but often goes undetected. The result may be that an unrelated value is modified. These errors are difficult to debug.

声明一个指针变量并不会自动分配任何内存。在对指针执行间接访问前,指针必须进行初始化:或者使它指向现有的内存,或者给它分配动态内存。对未初始化的指针变量执行间接访问操作是非法的,而且这种错误常常难以检测。其结果常常是一个不相关的值被修改。这种错误是很难被调试发现的。


The NULL pointer is a value that is defined as pointing to nothing at all. It can be assigned to a pointer variable to indicate that the variable does not point to any value. The result of applying indirection to a NULL pointer is implementation dependent; two common results are to return the value of memory location zero and to abort the program.

NULL 指针就是不指向任何东西的指针。它可以赋值给一个指针,用于表示那个指针并不指向任何值。对NULL 指针执行间接访问操作的后果因编译器而异,两个常见的后果分别是返回内存位置零的值以及终止程序。


A pointer variable, like any other variable, can be used as an L‐value. Applying indirection to a pointer also results in an L‐value because the expression identifies a specific memory location.

和任何其他变量一样,指针变量也可以作为左值使用。对指针执行间接访问操作所产生的值也是个左值,因为这种表达式标识了-个特定的内存位置。


Other than the NULL pointer, there is no built‐in notation for expressing pointer constants because the programmer usually has no way of predicting where the compiler will place variables. In the rare cases where they are needed, pointer constants can be created by casting integer values.

除了NULL 指针之外,再也没有任何内建的记法来表示指针常量,因为程序员通常无法预测编译器会把变量放在内存中的什么位置。在极少见的情况下,我们偶尔需要使用指针常量,这时我们可以通过把一个整型值强制转换为指针类型来创建它。


Limited arithmetic can be performed on pointer values. You can add an integer value to a pointer, and subtract an integer value from a pointer. In both cases, the integer value is scaled by the size of the pointerʹs target type. Thus, adding one to a pointer makes it point to the next variable regardless of the size of the variables in memory.

在指针值上可以执行一些有限的算术运算。你可以把一个整型值加到-个指针上,也可以从-个指针减去一个整型值。在这两种情况下,这个整型值会进行调整,原值将乘以指针目标类型的长度。这样,对一个指针加1 将使它指向下一个变量,至于该变量在内存中占几个字节的大小则与此无关。


However, this pointer arithmetic is predictable only with arrays. It is illegal (but often not detected) to perform arithmetic on any pointer that does not point to an element of an array. It is also illegal to subtract from a pointer if the result lies before the first element of the array to which it points, and to add to a pointer if the result lies more than one element after the end of the array to which it points. The result of a pointer addition may point one element beyond the end of the array, but it is illegal to apply indirection to this result.

然而,指针运算只有作用于数组中其结果才是可以预测的。对任何并非指向数组元素的指针执行算术运算是非法的(但常常很难被检测到)。如果-个指针减去一个整数后,运算结果产生的指针所指向的位置在数组第一个元素之前,那么它也是非法的。加法运算稍有不同,如果结果指针指向

数组最后一个元素后面的那个内存位置仍是合法(但不能对这个指针执行间接访问操作),不过再往后就不合法了。


Two pointers may be subtracted from one another only if they both point to elements of the same array. The result of pointer subtraction is scaled by the size of the values in the array, so the result is the number of values separating the original pointers. Subtracting pointers to elements of different arrays is an (often undetected) error.

如果两个指针都指向同→个数组中的元素,那么它们之间可以相减。指针减法的结果经过调整(除以数组元素类型的长度),表示两个指针在数组中相隔多少个元素。如果两个指针并不是指向同一个数组的元素,那么它们之间进行相减就是错误的。


Any pointers may be compared to each other for equality or inequality. Two pointers that point to elements of the same array may also be compared using the relational operators <, <=, >, and >= to determine their positions in the array relative to each other. The result of a relational comparison between unrelated pointers is undefined.

任何指针之间都可以进行比较,测试它们相等或不相等。如果两个指针都指向同一个数组中的元素,那么它们之间还可以执行〈、<=、〉和>=等关系运算,用于判断它们在数组中的相对位置。对两个不相关的指针执行关系运算,其结果是未定义的。

上一章 Pointers on C——6 Pointers.14

Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Contents A Quick Start ........................................................................................................ 1 Basic Concepts ...................................................................................................... 7 Data ....................................................................................................................... 11 Statements ............................................................................................................. 15 Operators and Expressions .................................................................................... 23 Pointers .................................................................................................................. 29 Functions ............................................................................................................... 37 Arrays .................................................................................................................... 43 Strings, Characters, and Bytes .............................................................................. 55 Structures and Unions ........................................................................................... 69 Dynamic Memory Allocation ................................................................................ 75 Using Structures and Pointers ............................................................................... 79 Advanced Pointer Topics ...................................................................................... 87 The Preprocessor ................................................................................................... 93 Input/Output Functions .......................................................................................... 95 Standard Library .................................................................................................... 119 Classic Abstract Data Types ................................................................................. 129 Runtime Environment ........................................................................................... 145
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值