char * strcat ( char * destination, const char * source );
目录
使用stract需要注意
1、源字符串必须以 '\0' 结束。
2、目标空间必须有足够的大,能容纳下源字符串的内容。
3、目标空间必须可修改。
strcat的使用
#include <stdio.h>
#include <string.h>
int main()
{
char arr[20] = "hello";
char str[] = "word";
strcat(arr, str);
printf("%s\n", arr);
return 0;
}
arr作为目的地
指向目标数组的指针,该数组应包含 C 字符串,并且足够大以包含串联的结果字符串。
str作为源
要追加的 C 字符串。这不应与目标重叠。
得到
strcat的模拟实现
源不能被修改便可以加上const,返回类型为char*的指针。
strcat的实现原理
即需要让destination的位置到\0,从\0的位置拷贝soure后的字符。
char* my_strcat(char* destination, const char* source)
{
char* ret = destination;
assert(destination && source);
while (*destination)
{
destination++;
}
while (*destination++ = *source++)
{
;
}
return destination;
}
可得到
需要 注意destination++并不能该成while (*destination++)