9.3.2 Concatenating Strings
To append (concatenate) one string to the end of another, strcat is used prototype is:
要想把一个字符串添加(连接〉到另一个字符串的后面,你可以使用strcat 函数。它的原型如下:
char *strcat( char *dst, char const *src );
strcat requires that dst already contain a (possibly empty) string. It finds the end of this string, and appends to it a copy of the string from src. If the src and dst strings overlap, the result is undefined.
strcat 函数要求dst 参数原先已经包含了一个字符串(可以是空字符串)。它找到这个字符串的末尾,并把src 字符串的一份拷贝添加到这个位置。如果src 和dst 的位置发生重叠,其结果是未定义的。
The following example shows a common use of this function.
下面这个例子显示了这个函数的一种常见用法。
strcpy( message, "Hello " );
strcat( message, customer_name );
strcat( message, ", how are you?" );
Each of the arguments to strcat are appended to the string already in message. The result is a string such as this one:
每个strcat 函数的字符串参数都被添加到原先存在于message 数组的字符串后面。其结果是下面这个字符串:
Hello Jim, how are you?
Once again, the programmer must ensure that there is enough space remaining in the destination array to hold the entire source string. But this time it is incorrect to simply compare the length of the source string with the size of the array. You must also account for the length of die existing string.
和前面一样,程序员必须保证目标字符数组剩余的空间足以保存整个源字符串。但这次并不是简单地把源字符串的长度和目标字符数组的长度进行比较,你必须考虑目标数组中原先存在的字符串。
9.3.3 Function Return Value
Both strcpy and strcat return a copy of their first argument, which is a pointer to the destination array. Because of this value, you can nest calls to these functions, as illustrated in the following example.
strcpy 和strcat都返回它们第1 个参数的一份拷贝,就是一个指向目标字符数组的指针。由于它们返回这种类型的值,所以你可以嵌套地调用这些函数,如下面的例子所示:
strcat( strcpy( dst, a ), b );
The strcpy is performed first. It copies the string from a into det and returns the value dst. The returned value becomes the first argument to strcat, which appends the string in b to dst.
strcpy 首先执行。它把字符串从a 复制到dst 并返回dst。然后这个返回值成为strcat函数的第1个参数, strcat 函数把b 添加到dst 的后面。
This nested style does not have a functional advantage over the more readable.
这种嵌套调用的风格较之下面这种可读性更佳的风格在功能上并无优势。
strcpy( dst, a );
strcat( dst, b );
Indeed, the values returned by the vast majority of calls to these functions are simply ignored.
事实上,在这些函数的绝大多数调用中,它们的返回值只是被简单地忽略。
9.3.4 String Comparisons
Comparing two strings involves comparing the corresponding characters in them, one by one, until a mismatch is found. The string from which the ʺlowerʺ character (that is,the character nearer the beginning of the character set) came is said to be ʺless thanʺ the other string. If one string is a prefix of the other, it will be ʺless thanʺ the other string because its terminating NUL is reached first. Called a lexicographic comparison,this process gives the same result as an everyday alphabetic ordering for strings containing only uppercase or only lowercase characters.
比较两个字符串涉及对两个字符串对应的字符逐个进行比较,直到发现不匹配为止。那个最先不匹配的字符中较"小" (也就是说,在字符集中的序数较小)的那个字符所在的字符串被认为"小于"另外一个字符串。如果其中一个字符串是另外一个字符串的前面一部分,那么它也被认为"小于"另外一个字符串,因为它的NUL 结尾字节出现得更早。这种比较被称为"词典比较",对于只包含大写字母或只包含小写字母的字符串比较,这种比较过程所给出的结果总是和我们日常所用的字母顺序的比较相同。
The library function strcmp compares two string arguments, its prototype is:
库函数strcmp 用于比较两个字符串,它的原型如下:
int strcmp( char const *s1, char const *s2 );
strcmp returns a value less than zero if s1 is less than s2; a value greater than zero if s1 is greater than s2; and zero if the two strings are equal.
如果s1 小于s2 , strcmp 函数返回一个小于零的值。如果s1 大于s2 ,函数返回一个大于零的值。如果两个字符串相等,函数就返回零。
Beginners often write
初学者常常会编写下面这样的表达式
if( strcmp( a, b ) )
and assume that the result returned will be true if the strings are equal. The result is just the opposite though, because zero (false) is returned in this case. However, it is bad style to test this value as if it were boolean because it has three distinct outcomes:less, equal, and greater. Comparing the value to zero is therefore preferred.
他以为如果两个字符串相等,它的结果将是真。但是,这个结果将正好相反,因为在两个字符串相等的情况下返回值是零(假)。然而,把这个返回值当作布尔值进行测试是一种坏风格,因为它具有三个截然不同的结果:小于、等于和大于。所以,更好的方法是把这个返回值与零进行比较。
Note that the Standard does not specify the specific values used to indicate inequalities. It only states that the value returned be greater than zero if the first string is greater than the second and be less than zero if the first string is less than the second. A common mistake is to assume that the values 1 and ‐1 will be returned, but this assumption is not always correct.
注意标准并没有规定用于提示不相等的具体值。它只是说如果第1 个字符串大于第2 个字符串就返回一个大于零的值,如采第1 个字符串小于第2 个字符串就返回一个小于零的值。一个常见的错误是以为返回值是1 和-1,分别代表大于和小于。但这个假设并不总是正确的。
Because strcmp does not change either of its arguments, there isnʹt any danger of overflowing an array. However, as with the other unrestricted string functions, the string arguments must be NUL terminated. If they are not, strcmp will continue comparing bytes beyond the end of the data, and the result will have no meaning.
由于strcmp 并不修改它的任何一个参数,所以不存在溢出字符数组的危险。但是,和其他不受限制的字符串函数一样, strcmp 函数的字符串参数也必须以一个NUL 字节结尾。如果并非如此,strcmp 就可能对参数后面的字节进行比较,这个比较结果将不会有什么意义。