赞
踩
目录
strcat函数所需要的头文件:
#include<string.h>
strcat函数的参数解析:
将 source 字符串追加到 destination 字符串。destination 中的字符串结束标志 '\0' 被 source 的第一个字符覆盖,source 字符串后面的字符依次向后追加,且 source 字符串的 '\0' 也要追加上
source 字符串的内容不会被改变,所以可加上 const 关键字修饰
strcat函数的返回值:
返回 destination 字符串的起始位置
注意:
destination 字符串的空间要足够大,能容纳下追加的 source 字符串,否则就会报错
- char* my_strcat(char* destination, const char* source)
- {
- // 断言
- assert(destination != NULL);
- assert(source != NULL);
-
- // 先保存目标字符串的首地址
- char* ret = destination;
-
- // 找到目标字符串的'\0'
- while (*destination)
- {
- destination++;
- }
-
- // 追加
- while (*source)
- {
- *destination++ = *source++;
- }
-
- // 返回目标字符串的首地址
- return ret;
- }

代码验证:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。