Pointers on C——9 Strings, Characters, and Bytes.4

​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 就可能对参数后面的字节进行比较,这个比较结果将不会有什么意义。

上一章 Pointers on C——9  Strings, Characters, and Bytes.3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值