Pointers on C——8 Arrays.17

来源:互联网 发布:mac系统最大化快捷键 编辑:程序博客网 时间:2024/05/19 13:58

​8.2.4 Pointers to Arrays

Are these declarations legal?

下面这些声明合法吗?

int vector[10], *vp = vector;

int matrix[3] [10], *mp = matrix;


The first is legal. It allocates a vector of integers, and then declares vp to be a pointer to an integer and initializes the pointer to point to the first element in vector. Both vector and vp have the same type: pointer to integer. The second declaration is illegal, however. It creates the matrix correctly and declares mp to be a pointer to an integer,but the initialization of mp is incorrect because matrix is not a pointer to an integer, it is a pointer to an array of integers. How would we declare a pointer to an array of integers?

第1 个声明是合法的。它为一个整型数组分配内存,并把vp 声明为一个指向整型的指针,并把它初始化为指向vector 数组的第1 个元素。vector 和vp 具有相同的类型:指向整型的指针。但是,第2 个声明是非法的。它正确地创建了matrix 数组,并把mp 声明为一个指向整型的指针。但是,mp 的初始化是不正确的,因为matrix 并不是一个指向整型的指针,而是一个指向整型数组的指针。我们应该怎样声明一个指向整型数组的指针的呢?


int (*p)[10];


This declaration is more complex than those you have seen before, but it really isnʹt too difficult. Just pretend that it is an expression and evaluate it. Subscripts have a higher precedence than indirection, but the parentheses surrounding the indirection force it to go first. So p is a pointer to something, but to what?

这个声明比我们以前见过的所有声明更为复杂,但它事实上并不是很难。你只要假定它是一个表达式并对它求值。下标引用的优先级高于间接访问,但由于括号的存在,首先执行的还是间接访问。所以, p 是个指针,但它指向什么呢?


The subscript is applied next, so p points to some kind of array. There arenʹt any more operators in the declaration expression, so each element of the array is an integer.

接下来执行的是下标引用,所以p 指向某种类型的数组。这个声明表达式中并没有更多的操作符,所以数组的每个元素都是整数。


The declaration doesnʹt tell you directly what p is, but it is not difficult to figure out—when we applied indirection to p we got an array, and putting a subscript on that expression produced an integer. So p is a pointer to an array of integers.

声明并没有直接告诉你p 是什么,但推断它的类型并不困难——当我们对它执行间接访问操作时,我们得到的是个数组,对该数组进行下标引用操作得到的是一个整型值。所以p 是一个指向整型数组的指针。


Adding initialization to the declaration gives us:

在声明中加上初始化后是下面这个样子:


int (*p)[10] = matrix;


which makes p point to the first row of matrix.

它使p 指向matrix 的第1 行。


p is a pointer to an array of ten integers. Adding an integer to p will cause the integer value to be scaled by the size of ten integers before the addition takes place. So we can use this pointer to step through the matrix row by row.

p 是一个指向拥有10 个整型元素的数组的指针。当你把p 与一个整数相加时,该整数值首先根据10 个整型值的长度进行调整,然后再执行加法。所以我们可以使用这个指针一行一行地在matrix中移动。


What should you do if you want a pointer that will go through the matrix integer by integer rather than row by row? Both of the following declarations create a simple integer pointer, initialized in two different ways to point to the first integer in the matrix.

如果你需要一个指针逐个访问整型元素而不是逐行在数组中移动,你应该怎么办呢?下面两个声明都创建了一个简单的整型指针,并以两种不同的方式进行初始化,指向matrix 的第1 个整型元素。


int *pi = &matrix[0][0];

int *pi = matrix[0];//matrix[0]+0


Incrementing this pointer advances it to the next integer.

增加这个指针的值使它指向下一个整型元素。

If you intend to perform any arithmetic with the pointer, avoid this kind of declaration:

如采你打算在指针上执行任何指针运算,应该避免这种类型的声明:


int (*p)[] = matrix;


p is still a pointer to an array of integers, but the array size is missing. Integers involved in pointer arithmetic with this variable will be scaled by the size of an empty array (that is, multiplied by zero), which is probably not what you had in mind. Some compilers catch this error and some donʹt.

p 仍然是一个指向整型数组的指针,但数组的长度却不见了。当某个整数与这种类型的指针执行指针运算时,它的值将根据空数组的长度进行调整(也就是说,与零相乘) ,这很可能不是你所设想的。有些编译器可以捕捉到这类错误,但有些编译器却不能。


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

原创粉丝点击