Pointers on C——6 Pointers.12

来源:互联网 发布:淘宝网代卖怎么做的 编辑:程序博客网 时间:2024/06/18 03:56

​6.12 Examples

Here are some sample programs to illustrate a few common pointer expressions.Program 6.1 computes the length of a string. You should never have to write this function because the library contains one, but it is a useful example.

这里有几个例子程序,用于说明指针表达式的→些常见用法。程序6.1 计算一个字符串的长度。你应该不用自己编写这个函数,因为函数库里已经有了一个,不过它是个有用的例子。

/*

** Compute the length of a string.

*/

#include <stdlib.h>

size_t strlen( char *string )

{

int length = 0;

/*

** Advance through the string, counting characters

** until the terminating NUL byte is reached.

*/

while( *string++ != '\0' )

length += 1;

return length;

}

Program 6.1 String length

The expression *string++ in the while statement is true until the pointer has reached the terminating NUL byte. It also increments the pointer for the next test. This expression even handles empty strings properly.

在指针到达字符串末尾的NUL 字节之前, while 语句中*string++ 表达式的值一直为真。它同时增加指针的值,用于下一次测试。这个表达式甚至可以正确地处理空字符串。


If the function is called with a NULL pointer, the indirection in the while statement will fail. Should the function check for this condition before dereferencing the pointer? To be absolutely safe, it should. However, this function does not create the string. If it finds that the argument is NULL, it has really found an error that occurred elsewhere in the program. It seems logical that the pointer should be checked where it was created, because then it need be checked only once. This approach is taken by this function. If the function fails because a careless client didnʹt bother to check the argument, they deserve what they get.

如果这个函数调用时传递给它的是一个NULL 指针,那么while 语句中的间接访问将会失败。函数是不是应该在解引用指针前检查这个条件?从绝对安全的角度讲,应该如此。但是,这个函数并不负责创建字符串。如采它发现参数为NULL ,它肯定发现了一个出现在程序其他地方的辛苦误。当指针创建时检查它是否有效是合乎逻辑的,因为这样只需检查一次。这个函数采用的就是这种方法。如果函数失败是因为粗心大意的调用者懒得检查参数的有效性而引起的,那是他活该如此。


Programs 6.2 and 6.3 add a level of indirection. They search a collection of strings for a specific character value, but an array of pointers is used to keep track of the strings, as illustrated in Figure 6.1. The arguments to the functions are strings, the pointer to the array of pointers; and value, the character value weʹre looking for.Notice that the array of pointers ends with a NULL pointer. The functions will check for this value to determine when to stop. The expression in

程序6.2 和6 .3增加了一层间接访问。它们在一些字符串中搜索某个特定的字符值,但我们使用指针数组来表示这些字符串,如图6.1 所示。函数的参数是strings 和value , strings 是一个指向指针数组的指针, value 是我们所查找的字符值。注意指针数组以一个NULL 指针结束。函数将检查这个值以判断循环何时结束。下面这行表达式


while( ( string = *strings++ ) != NULL )


does three things: (1) it copies the pointer that strings currently points to into the variable string, (2) it increments strings to point to the next value, and (3) it tests whether string is NULL. The inner while loop stops when string points to the terminating NUL byte at the end of the current string.

完成三项任务:(1)它把strings 当前所指向的指针复制到变量string 中。(2) 它增加strings 的值,使它指向下一个值。(3) 它测试string 是否为NULL 。当string 指向当前字符串中作为终止标志的NUL字节时,内层的while 循环就终止。


/*

** Given a pointer to a NULL-terminated list of pointers, search

** the strings in the list for a particular character.

*/

#include <stdio.h>

#define TRUE 1

#define FALSE 0

int find_char( char **strings, char value )

{

char *string; /* the string we're looking at */

/*

** For each string in the list ...

*/

while( ( string = *strings++ ) != NULL ){

/*

** Look at each character in the string to see if

** it is the one we want.

*/

while( *string != '\0' ){

if( *string++ == value )

return TRUE;

}

}

return FALSE;

}

Program 6.2 Search a collection of strings: version one s_srch1.c

Until then,

if( *string++ == value )


tests whether the current character matches the desired value and increments the pointer to the next character.

它测试当前的字符是否与需要查找的字符匹配,然后增加指针的值,使它指向下一个字符。


Program 6.3 performs the same function without making a copy of the pointer to each string. As a side effect, however, this program destroys the array of pointers.This side effect makes the function less useful than the previous version, as it can be used only when the strings are searched once.

程序6.3实现相同的功能,但它不需要对指向每个字符串的指针作一份拷贝。但是,由于存在副作用,这个程序将破坏这个指针数组。这个副作用使该函数不如前面那个版本有用,因为它只适用于字符串只需要查找一次的情况。

/*

** Given a pointer to a NULL-terminated list of pointers, search

** the strings in the list for a particular character. This

** version destroys the pointers so it can only be used when

** the collection will be examined only once.

*/

#include <stdio.h>

#include <assert.h>

#define TRUE 1

#define FALSE 0

int find_char( char **strings, int value )

{

assert( strings != NULL );//指针数组的地址

/*

** For each string in the list ...

*/

while( *strings != NULL ){//数组中每个指针元素

/*

** Look at each character in the string to see if

** it is the one we want.

*/

while( **strings != '\0' ){//字符串中的每个字符

if( *(*strings)++ == value )

return TRUE;

}

strings++;

}

return FALSE;

}

Program 6.3 Search a collection of strings: version two s_srch2.c


There are, however, two interesting expressions in Program 6.3. The first is **strings.The first indirection goes to the current pointer in the array of pointers, and the second follows that pointer to the current character in the string. The inner while statementtests this character to see whether the end of the string has been reached.

但是,在程序6 .3中存在两个有趣的表达式。第1 个是**strings 。第1 个间接访问操作访问指针数组中的当前指针,第2 个间接访问操作随该指针访问字符串中的当前字符。内层的while 语句测试这个字符的值并观察是否到达了字符串的末尾。


The second interesting expression is* (*strings)++. Parentheses are needed in order for the expression to evaluate properly. The first indirection goes to the current pointer in the list. The increment adds one to that location, but the second indirection works on a copy of the original value. The net result is that the current character in the current string is tested to see whether weʹve reached the end of the string, and the pointer to the current string character is incremented as a side effect.

第2 个有趣的表达式是* (*strings)++。括号是需要的,这样才能使表达式以正确的顺序进行求值。第1 个间接访问操作访问列表中的当前指针。增值操作把该指针所指向的那个位置的值加1,但第2个间接访问操作作用于原先那个值的拷贝上。这个表达式的直接作用是对当前字符串中的当前字符进行测试,看看是否到达了字符串的末尾。作为副作用,指向当前字符串字符的指针值将增加1 。

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