模拟实现strcat
strcat()函数是char*类型,其主要作用是将第二个字符串拷贝到第一个字符串后,形成一个新的字符串。
但需要注意的是strcat()函数并不能检查出第一个数组是否能容纳下第二个字符串,可能会出现多出来的字符溢出问题。所以在使用的过程一定要考虑到为第一个数组分配足够大的空间,或者可以使用需要另一个参数来指定最大允许添加的字符数的strncat()函数。
#define SIZE 80
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
int main(void)
{
char name[SIZE];
char str[] = "'s pen is red";
puts ("Whose pen is this?");
gets(name);
strcat(name, str);
puts(name);
puts(str);
return 0;
}
代码运行图如下:
仍有问题:<警告>gets未定义,假设外部返回为int