⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter5.html

📁 Kernighan and Ritchie - The C Programming Language c程序设计语言(第二版)称作是C语言学习的圣经
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html><head><title>Chapter 5 - Pointers and Arrays</title></head><body><hr><p align="center"><a href="chapter4.html">Back to Chapter 4</a>&nbsp;--&nbsp;<a href="kandr.html">Index</a>&nbsp;--&nbsp;<a href="chapter6.html">Chapter 6</a><p><hr><h1>Chapter 5 - Pointers and Arrays</h1>A pointer is a variable that contains the address of a variable. Pointers aremuch used in C, partly because they are sometimes the only way to express acomputation, and partly because they usually lead to more compact and efficientcode than can be obtained in other ways. Pointers and arrays are closelyrelated; this chapter also explores this relationship and shows how to exploitit.<p>Pointers have been lumped with the <tt>goto</tt> statement as a marvelous way tocreate impossible-to-understand programs. This is certainly true when they areused carelessly, and it is easy to create pointers that point somewhereunexpected. With discipline, however, pointers can also be used to achieveclarity and simplicity. This is the aspect that we will try to illustrate.<p>The main change in ANSI C is to make explicit the rules about how pointers canbe manipulated, in effect mandating what good programmers already practice andgood compilers already enforce. In addition, the type <tt>void *</tt> (pointer to<tt>void</tt>) replaces <tt>char *</tt> as the proper type for a generic pointer.<h2><a name="s5.1">5.1 Pointers and Addresses</a></h2>Let us begin with a simplified picture of how memory is organized. A typicalmachine has an array of consecutively numbered or addressed memory cells thatmay be manipulated individually or in contiguous groups. One common situationis that any byte can be a <tt>char</tt>, a pair of one-byte cells can be treatedas a <tt>short</tt> integer, and four adjacent bytes form a <tt>long</tt>. A pointeris a group of cells (often two or four) that can hold an address. So if <tt>c</tt>is a <tt>char</tt> and <tt>p</tt> is a pointer that points to it, we could representthe situation this way:<p align="center"><img src="pic51.gif"><p>The unary operator <tt>&amp;</tt> gives the address of an object, so thestatement<pre>   p = &c;</pre>assigns the address of <tt>c</tt> to the variable <tt>p</tt>, and <tt>p</tt>is said to ``point to'' <tt>c</tt>. The <tt>&amp;</tt> operator only appliesto objects in memory: variables and array elements. It cannot be applied toexpressions, constants, or <tt>register</tt> variables.<p>The unary operator <tt>*</tt> is the <em>indirection</em> or<em>dereferencing</em> operator; when applied to a pointer, it accesses theobject the pointer points to. Suppose that <tt>x</tt> and <tt>y</tt> areintegers and <tt>ip</tt> is a pointer to <tt>int</tt>. This artificialsequence shows how to declare a pointer and how to use <tt>&amp;</tt> and<tt>*</tt>:<pre>   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] */</pre>The declaration of <tt>x</tt>, <tt>y</tt>, and <tt>z</tt> are what we've seenall along. The declaration of the pointer <tt>ip</tt>,<pre>   int *ip;</pre>is intended as a mnemonic; it says that the expression <tt>*ip</tt> is an<tt>int</tt>. The syntax of the declaration for a variable mimics the syntaxof expressions in which the variable might appear. This reasoning applies tofunction declarations as well. For example,<pre>   double *dp, atof(char *);</pre>says that in an expression <tt>*dp</tt> and <tt>atof(s)</tt> have values of<tt>double</tt>, and that the argument of <tt>atof</tt> is a pointer to<tt>char</tt>.<p>You should also note the implication that a pointer is constrained to pointto a particular kind of object: every pointer points to a specific data type.(There is one exception: a ``pointer to <tt>void</tt>'' is used to hold any typeof pointer but cannot be dereferenced itself. We'll come back to it in<a href="#s5.11">Section 5.11</a>.)<p>If <tt>ip</tt> points to the integer <tt>x</tt>, then <tt>*ip</tt> can occurin any context where <tt>x</tt> could, so<pre>   *ip = *ip + 10;</pre>increments <tt>*ip</tt> by 10.<p>The unary operators <tt>*</tt> and <tt>&amp;</tt> bind more tightly thanarithmetic operators, so the assignment<pre>   y = *ip + 1</pre>takes whatever <tt>ip</tt> points at, adds 1, and assigns the result to<tt>y</tt>, while<pre>   *ip += 1</pre>increments what <tt>ip</tt> points to, as do<pre>   ++*ip</pre>and<pre>   (*ip)++</pre>The parentheses are necessary in this last example; without them, theexpression would increment <tt>ip</tt> instead of what it points to, becauseunary operators like <tt>*</tt> and <tt>++</tt> associate right to left.<p>Finally, since pointers are variables, they can be used withoutdereferencing. For example, if <tt>iq</tt> is another pointer to <tt>int</tt>,<pre>   iq = ip</pre>copies the contents of <tt>ip</tt> into <tt>iq</tt>, thus making <tt>iq</tt>point to whatever <tt>ip</tt> pointed to.<h2><a name="s5.2">5.2 Pointers and Function Arguments</a></h2>Since C passes arguments to functions by value, there is no direct way forthe called function to alter a variable in the calling function. Forinstance, a sorting routine might exchange two out-of-order arguments with afunction called <tt>swap</tt>. It is not enough to write<pre>   swap(a, b);</pre>where the <tt>swap</tt> function is defined as<pre>   void swap(int x, int y)  /* WRONG */   {       int temp;       temp = x;       x = y;       y = temp;   }</pre>Because of call by value, <tt>swap</tt> can't affect the arguments <tt>a</tt>and <tt>b</tt> in the routine that called it. The function above swaps<em>copies</em> of <tt>a</tt> and <tt>b</tt>.<p>The way to obtain the desired effect is for the calling program to pass<em>pointers</em> to the values to be changed:<pre>   swap(&a, &b);</pre>Since the operator <tt>&amp;</tt> produces the address of a variable,<tt>&amp;a</tt> is a pointer to <tt>a</tt>. In <tt>swap</tt> itself, theparameters are declared as pointers, and the operands are accessed indirectlythrough them.<pre>   void swap(int *px, int *py)  /* interchange *px and *py */   {       int temp;       temp = *px;       *px = *py;       *py = temp;   }</pre>Pictorially:<p align="center"><img src="pic52.gif"><p>Pointer arguments enable a function to access and change objects in thefunction that called it. As an example, consider a function <tt>getint</tt> thatperforms free-format input conversion by breaking a stream of characters intointeger values, one integer per call. <tt>getint</tt> has to return the value itfound and also signal end of file when there is no more input. These valueshave to be passed back by separate paths, for no matter what value is usedfor <tt>EOF</tt>, that could also be the value of an input integer.<p>One solution is to have <tt>getint</tt> return the end of file status as itsfunction value, while using a pointer argument to store the converted integerback in the calling function. This is the scheme used by <tt>scanf</tt> aswell; see <a href="chapter7.html#s7.4">Section 7.4</a>.<p>The following loop fills an array with integers by calls to <tt>getint</tt>:<pre>   int n, array[SIZE], getint(int *);   for (n = 0; n &lt; SIZE && getint(&array[n]) != EOF; n++)       ;</pre>Each call sets <tt>array[n]</tt> to the next integer found in the input andincrements <tt>n</tt>. Notice that it is essential to pass the address of<tt>array[n]</tt> to <tt>getint</tt>. Otherwise there is no way for <tt>getint</tt> tocommunicate the converted integer back to the caller.<p>Our version of <tt>getint</tt> returns <tt>EOF</tt> for end of file, zero ifthe next input is not a number, and a positive value if the input contains avalid number.<pre>   #include &lt;ctype.h&gt;   int getch(void);   void ungetch(int);   /* getint:  get next integer from input into *pn */   int getint(int *pn)   {       int c, sign;       while (isspace(c = getch()))   /* skip white space */           ;       if (!isdigit(c) && c != EOF && c != '+' && c != '-') {           ungetch(c);  /* it is not a number */           return 0;       }       sign = (c == '-') ? -1 : 1;       if (c == '+' || c == '-')           c = getch();       for (*pn = 0; isdigit(c), c = getch())           *pn = 10 * *pn + (c - '0');       *pn *= sign;       if (c != EOF)           ungetch(c);       return c;   }</pre>Throughout <tt>getint</tt>, <tt>*pn</tt> is used as an ordinary <tt>int</tt>variable. We have also used <tt>getch</tt> and <tt>ungetch</tt> (described in<a href="chapter4.html#s4.3">Section 4.3</a>) so the one extra character thatmust be read can be pushed back onto the input.<p><strong>Exercise 5-1.</strong> As written, <tt>getint</tt> treats a<tt>+</tt> or <tt>-</tt> not followed by a digit as a valid representation ofzero. Fix it to push such a character back on the input.<p><strong>Exercise 5-2.</strong> Write <tt>getfloat</tt>, the floating-pointanalog of <tt>getint</tt>. What type does <tt>getfloat</tt> return as itsfunction value?<h2><a name="s5.3">5.3 Pointers and Arrays</a></h2>In C, there is a strong relationship between pointers and arrays, strongenough that pointers and arrays should be discussed simultaneously. Anyoperation that can be achieved by array subscripting can also be done withpointers. The pointer version will in general be faster but, at least to theuninitiated, somewhat harder to understand.<p>The declaration<pre>   int a[10];</pre>defines an array of size 10, that is, a block of 10 consecutive objects named<tt>a[0]</tt>, <tt>a[1]</tt>, ...,<tt>a[9]</tt>.<p align="center"><img src="pic53.gif"><p>The notation <tt>a[i]</tt> refers to the <tt>i</tt>-th element of the array.If <tt>pa</tt> is a pointer to an integer, declared as<pre>   int *pa;</pre>then the assignment<pre>   pa = &a[0];</pre>sets <tt>pa</tt> to point to element zero of <tt>a</tt>; that is, <tt>pa</tt>contains the address of <tt>a[0]</tt>.<p align="center"><img src="pic54.gif"><p>Now the assignment<pre>   x = *pa;</pre>will copy the contents of <tt>a[0]</tt> into <tt>x</tt>.<p>If <tt>pa</tt> points to a particular element of an array, then by definition<tt>pa+1</tt> points to the next element, <tt>pa+i</tt> points <tt>i</tt>elements after <tt>pa</tt>, and <tt>pa-i</tt> points <tt>i</tt> elementsbefore. Thus, if <tt>pa</tt> points to <tt>a[0]</tt>,<pre>   *(pa+1)</pre>refers to the contents of <tt>a[1]</tt>, <tt>pa+i</tt> is the address of<tt>a[i]</tt>, and <tt>*(pa+i)</tt> is the contents of <tt>a[i]</tt>.<p align="center"><img src="pic55.gif"><p>These remarks are true regardless of the type or size of the variables in thearray <tt>a</tt>. The meaning of ``adding 1 to a pointer,'' and by extension,all pointer arithmetic, is that <tt>pa+1</tt> points to the next object, and<tt>pa+i</tt> points to the <tt>i</tt>-th object beyond <tt>pa</tt>.<p>The correspondence between indexing and pointer arithmetic is very close. Bydefinition, the value of a variable or expression of type array is theaddress of element zero of the array. Thus after the assignment<pre>   pa = &a[0];</pre><tt>pa</tt> and <tt>a</tt> have identical values. Since the name of an arrayis a synonym for the location of the initial element, the assignment<tt>pa=&amp;a[0]</tt> can also be written as<pre>   pa = a;</pre>Rather more surprising, at first sight, is the fact that a reference to<tt>a[i]</tt> can also be written as <tt>*(a+i)</tt>. In evaluating<tt>a[i]</tt>, C converts it to <tt>*(a+i)</tt> immediately; the two formsare equivalent. Applying the operator <tt>&amp;</tt> to both parts of thisequivalence, it follows that <tt>&amp;a[i]</tt> and <tt>a+i</tt> are alsoidentical: <tt>a+i</tt> is the address of the <tt>i</tt>-th element beyond<tt>a</tt>. As the other side of this coin, if <tt>pa</tt> is a pointer,expressions might use it with a subscript; <tt>pa[i]</tt> is identical to<tt>*(pa+i)</tt>. In short, an array-and-index expression is equivalent toone written as a pointer and offset.<p>There is  one difference between an array name and a pointer that must bekept in mind. A pointer is a variable, so <tt>pa=a</tt> and <tt>pa++</tt> arelegal. But an array name is not a variable; constructions like <tt>a=pa</tt>and <tt>a++</tt> are illegal.<p>When an array name is passed to a function, what is passed is the location ofthe initial element. Within the called function, this argument is a localvariable, and so an array name parameter is a pointer, that is, a variablecontaining an address. We can use this fact to write another version of<tt>strlen</tt>, which computes the length of a string.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -