Chapter 5 - Pointers and Arrays(一)

xiaoxiao2021-02-28  85

A pointer is a variable that contains the address of a variable. Pointers are much used in C, partly because they are sometimes the only way to express a computation, and partly because they usually lead to more compact and efficient code than can be obtained in other ways. Pointers and arrays are closely related; this chapter also explores this relationship and shows how to exploit it.

指针是一种保存变量地址的变量。在C 语言中,指针的使用非常广泛,原因之一是,指针常常是表达某个计算的惟一途径,另一个原因是,同其它方法比较起来,使用指针通常可以生成更高效、更紧凑的代码。指针与数组之间的关系十分密切,我们将在本章中讨论它们之间的关系,并探讨如何利用这种关系。

Pointers have been lumped with the goto statement as a marvelous way to create impossible-to-understand programs. This is certainly true when they are used carelessly, and it is easy to create pointers that point somewhere unexpected. With discipline, however, pointers can also be used to achieve clarity and simplicity. This is the aspect that we will try to illustrate.

指针和 goto语句一样,会导致程序难以理解。如果使用者粗心,指针很容易就指向了错误的地方。但是,如果谨慎地使用指针,便可以利用它写出简单、清晰的程序。在本章中我们将尽力说明这一点。

The main change in ANSI C is to make explicit the rules about how pointers can be manipulated, in effect mandating what good programmers already practice and good compilers already enforce. In addition, the type void * (pointer to void) replaces char * as the proper type for a generic pointer. 

ANSI C的一个最重要的变化是,它明确地制定了操纵指针的规则。事实上,这些规则已经被很多优秀的程序设计人员和编译器所采纳。此外,ANSI C使用类型void *(指向void的指针)代替char *作为通用指针的类型。

5.1 Pointers and Addresses

Let us begin with a simplified picture of how memory is organized. A typical machine has an array of consecutively numbered or addressed memory cells that may be manipulated individually or in contiguous groups. One common situation is that any byte can be a char, a pair of one-byte cells can be treated as a short integer, and four adjacent bytes form a long. A pointer is a group of cells (often two or four) that can hold an address. So if c is a char and p is a pointer that points to it, we could represent the situation this way:

首先,我们通过一个简单的示意图来说明内存是如何组织的。通常的机器都有一系列连 续编号或编址的存储单元,过些存储单元可以单个进行操纵,也可以以连续成组的方式操纵。 通常情况下,机器的一个字节可以存放一个char类型的数据,两个相邻的字节存储单元可存 储一个short(短整型)类型的数据,而4 个相邻的字节存储单元可存储一个long(长整型) 类型的数据。指针是能够存放一个地址的一组存储单元(通常是两个或4个字节)。因此,如 果c的类型是char,并且p是指向c的指针,则可用图5-1表示它们之间的关系:

 

The unary operator & gives the address of an object, so the statement

一元运算符&可用于取一个对象的地址,因此,下列语句:

p = &c;

assigns the address of c to the variable p, and p is said to ``point to'' c. The & operator only applies to objects in memory: variables and array elements. It cannot be applied to expressions, constants, or register variables.

将把c的地址赋值给变量p,我们称p为“指向”c的指针。地址运算符&只能应用于内存中的对象,即变量与数组元素。它不能作用于表达式、常量或register类型的变量。

The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to. Suppose that x and y are integers and ip is a pointer to int. This artificial sequence shows how to declare a pointer and how to use & and *:

一元运算符*是间接寻址或间接引用运算符。当它作用于指针时,将访问指针所指向的对象。我们在这里假定x y 是整数,而ip 是指向int 类型的指针,下面的代码段说明了如何在程序中声明指针以及如何使用运算符&*

int x = 1, y = 2, z[10];

int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */

y = *ip; /* y is now 1 */

*ip = 0; /* x is now 0 */

ip = &z[0]; /* ip now points to z[0] */

The declaration of x, y, and z are what we've seen all along. The declaration of the pointer ip,

int *ip;

is intended as a mnemonic; it says that the expression *ip is an int. The syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear. This reasoning applies to function declarations as well. For example,

这样声明是为了便于记忆。该声明语句表明表达式*ip 的结果是int 类型。这种声明变量的语法与声明该变量所在表达式的语法类似。同样的原因,对函数的声明也可以采用这种方式。例如,声明

double *dp, atof(char *);

says that in an expression *dp and atof(s) have values of double, and that the argument of atof is a pointer to char.

表明,在表达式中,*dpatof(s)的值都是double类型,且atof的参数是一个指向char类型的指针。

You should also note the implication that a pointer is constrained to point to a particular kind of object: every pointer points to a specific data type. (There is one exception: a ``pointer to void'' is used to hold any type of pointer but cannot be dereferenced itself. We'll come back to it in Section 5.11.)

我们应该注意,指针只能指向某种特定类型的对象,也就是说,每个指针都必须指向某种特定的数据类型。(一个例外情况是指向void 类型的指针可以存放指向任何类型的指针,但它不能间接引用其自身。我们将在5.11节中详细讨论该问题)。

If ip points to the integer x, then *ip can occur in any context where x could, so

如果指针ip指向整型变量,那么在x可以出现的任何上下文中都可以使用*ip,因此,语句

*ip = *ip + 10;

increments *ip by 10.

将把*ip的值增加10

The unary operators * and & bind more tightly than arithmetic operators, so the assignment

y = *ip + 1

takes whatever ip points at, adds 1, and assigns the result to y, while

*ip += 1

increments what ip points to, as do

++*ip

and

(*ip)++

The parentheses are necessary in this last example; without them, the expression would increment ip instead of what it points to, because unary operators like * and ++ associate right to left.

语句的执行结果。语句(*ip)++中的圆括号是必需的,否则,该表达式将对ip进行加1 运算,而不是对ip指向的对象进行加1运算,这是因为,类似于*++这样的一元运算符遵循从右至左的结合顺序。

Finally, since pointers are variables, they can be used without dereferencing. For example, if iq is another pointer to int,

最后说明一点,由于指针也是变量,所以在程序中可以直接使用,而不必通过间接引用的方法使用。例如,如果iq是另一个指向整型的指针,那么语句

iq = ip

copies the contents of ip into iq, thus making iq point to whatever ip pointed to.

将把ip中的值拷贝到iq中,这样,指针iq也将指向ip指向的对象。

 

5.2 Pointers and Function Arguments

Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function. For instance, a sorting routine might exchange two out-of-order arguments with a function called swap. It is not enough to write

由于C 语言是以传值的方式将参数值传递给被调用函数。因此,被调用函数不能直接修改主调函数中变最的值。例如,排序函数可能会使用一个名为swap的函数来交换两个次序颠倒的元素。但是,如果将swap函数定义为下列形式:

swap(a, b);

where the swap function is defined as

void swap(int x, int y) /* WRONG */

{

int temp;

temp = x;

x = y;

y = temp;

}

Because of call by value, swap can't affect the arguments a and b in the routine that called it. The function above swaps copies of a and b.

The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed:

这是因为,由于参数传递采用传值方式,因此上述的swap函数不会影响到调用它的例程中的参数ab的值。该函数仅仅交换了ab的副本的值。

swap(&a, &b);

Since the operator & produces the address of a variable, &a is a pointer to a. In swap itself, the parameters are declared as pointers, and the operands are accessed indirectly through them.

由于一元运算符&用来取变量的地址,这样&a就是一个指向变量a的指针。swap函数的所有参数都声明为指针,并且通过这些指针来间接访问它们指向的操作数。

void swap(int *px, int *py) /* interchange *px and *py */

{

int temp;

temp = *px;

*px = *py;

*py = temp;

}

Pictorially:

consecutively  adv. 连续地

contiguous adj. 接触的,邻近的,共同的

转载请注明原文地址: https://www.6miu.com/read-22205.html

最新回复(0)