Pointers on C——8 Arrays.22

来源:互联网 发布:mac jenkins 如何重启 编辑:程序博客网 时间:2024/05/18 11:46

8.4 Summary


The value of an array name in most expressions is a pointer to the first element of the array. There are only two exceptions to this rule, sizeof returns the number of bytes in the entire array rather than the size of a pointer. The unary & operator returns a pointer to the first element of the array, not a pointer to a pointer to the first element.

在绝大多数表达式中,数组名的值是指向数组第1 个元素的指针。这个规则只有两个例外。sizeof返回整个数组所占用的字节而不是一个指针所占用的字节。单目操作符&返回一个指向数组的指针,而不是一个指向数组第1 个元素的指针的指针。


Except for their precedence, the subscript expression array[value] is the same as the indirection expression *( array + ( value ) ). Thus, subscripts may be used with pointer expressions as well as with array names, which makes it difficult for the compiler to check the validity of subscripts. Pointer expressions may be more efficient than subscripts, but subscripts are never more efficient than pointer expressions.However, reducing program, maintainability to gain runtime efficiency is rarely a good idea.

除了优先级不同以外,下标表达式array[value] 和间接访问表达式*(array+(value)) 是一样的。因此,下标不仅可以用于数组名,也可以用于指针表达式中。不过这样一来,编译器就很难检查下标的有效性。指针表达式可能比下标表达式效率更高,但下标表达式绝不可能比指针表达式效率更高。但是,以牺牲程序的可维护性为代价获得程序的运行时效率的提高可不是个好主意。


Pointers and arrays are not equivalent. The properties of an array and a pointer variable are quite different. Declaring an array creates space to store the array elements, while declaring a pointer variable only creates space to store the pointer.

指针和数组并不相等。数组的属性和指针的属性大相径庭。当我们声明一个数组时,它同时也分配了一些内存空间,用于容纳数组元素。但是,当我们声明一个指针时,它只分配了用于容纳指针本身的空间。


When an array name is passed as a function argument, a pointer to the first element is passed to the function. This parameter is a copy, so the function can manipulate it without affecting the actual argument. However, applying indirection to the pointer parameter allows the function to modify the original array elements.Array parameters may be declared either as array names or pointers; both declaration forms are equivalent for function parameters only.

当数组名作为函数参数传递时,实际传递给函数的是一个指向数组第1 个元素的指针。函数所接收到的参数实际上是原参数的一份拷贝,所以函数可以对其进行操纵而不会影响实际的参数。但是,对指针参数执行间接访问操作允许函数修改原先的数组元素。数组形参既可以声明为数组,也可以声明为指针。这两种声明形式只有当它们作为函数的形参时才是相等的。


An array may be initialized with a list of values enclosed in braces. Static variables (including arrays) receive their initial values when the program is loaded into memory. Automatic variables (including arrays) must be reinitialized with implicit assignment statements each time execution enters the block in which they are declared. If an initializer list has fewer values than the size of the array, the last elements of the array are initialized with the default value. If the dimension of an initialized array is omitted, the compiler will make the array just large enough to hold the values given in the initializer list. Character arrays may be initialized with a shorthand notation that resembles a string literal.

数组也可以用初始值列表进行初始化,初始值列表就是由一对花括号包围的一组值。静态变量(包括数组)在程序载入到内存时得到初始值。自动变量(包括数组)每次当执行流进入它们声明所在的代码块时都要使用隐式的赋值语句重新进行初始化。如果初始值列表包含的值的个数少于数组元素的个数,数组最后几个元素就用缺省值进行初始化。如果一个被初始化的数组的长度在声明中未给出,编译器将使这个数组的长度设置为刚好能容纳初始值列表中所有值的长度。字符数组也可以用一种很像字符串常量的快速方法进行初始化。


Multidimensional arrays are actually one‐dimensional arrays whose elements are each an array. The values in multidimensional arrays are stored in row major order, with the rightmost subscript varying most rapidly. The value of a multidimensional array name is a pointer to its first element, which will be a pointer to an array. Arithmetic with this pointer will be scaled by the size of the array. Subscripts for multidimensional arrays are also pointer expressions. When the name of a multidimensional array is passed as an argument to a function, the corresponding function parameter must be declared with the sizes of the second and subsequent dimensions. Because a multidimensional array is really a one‐dimensional array of complex elements, the initializer list for a multidimensional array contains a list of initializers for the complex elements. Each of these lists may also contain nested lists,as needed by the number of dimensions in the array. The interior braces may be omitted if the initializer lists are all complete. Only the first dimension of a multidimensional array may be automatically sized from the initializer list.

多维数组实际上是一维数组的一种特型,就是它的每个元素本身也是一个数组。多维数组中的元素根据行主序进行存储,也就是最右边的下标率先变化。多维数组名的值是一个指向它第1 个元素的指针,也就是一个指向数组的指针。对该指针进行运算将根据它所指向数组的长度对操作数进行调整。多维数组的下标引用也是指针表达式。当一个多维数组名作为参数传递给一个函数时,它所对应的函数形参的声明中必须显式指明第2 维(和接下去所有维)的长度。由于多维数组实际上是复杂元素的一维数组,一个多维数组的初始化列表就包含了这些复杂元素的值。这些值的每一个都可能包含嵌套的初始值列表,由数组各维的长度决定。如果多维数组的初始化列表是完整的,它的内层花括号可以省略。在多维数组的初始值列表中,只有第1 维的长度会被自动计算出来。


It is possible to create arrays of pointers. Lists of strings may be stored in a matrix, or as a list of pointers to vectors or string literals. In a matrix, each row must be as long as the longest string, however no pointers are stored. The list of pointers takes space, but each of the strings need only be as long as necessary.

我们还可以创建指针数组。字符串的列表可以以矩阵的形式存储,也可以以指向字符串常量的指针数组形式存储。在矩阵中,每行必须与最长字符串的长度一样长,但它不需要任何指针。指针数组本身要占用空间,但每个指针所指向的字符串所占用的内存空间就是字符串本身的长度。


上一章 Pointers on C——8 Arrays.21

原创粉丝点击