?
各位看官们,大家好,上一回中咱们说的是字符串概述的例子,这一回咱们说的例子是:字符串复制。闲
话休提,言归正转。让我们一起talk C栗子吧!
?
看官们,在C语言的标准库中为我们提供了字符串复制函数,我们只需要包含string.h头文件就可以使用
字符串复制函数。标准库提供了四个字符串复制函数:strcpy,strncpy,memcpy,memmove。接下来我们
分别介绍它们的用法和使用时的注意事项。
?
strcpy函数原型:char * strcpy(char *s1, const char *s2)strcpy函数用法:它把s2中内容复制到s1中,并且返回s1.strcpy注意事项:如果s2中的内容太多,以至于超过s1的容量,那么会覆盖掉s1后面内存中的内容。
?
我们举一个实际的例子来说明,在程序中定义如下字符串:
?
char s0[SIZE] = string;
char s1[SIZE]=str-1;
char s2[SIZE+3]=str-2and123;
char s3[SIZE] = {'A','B'};// other element of s3 is
char s4[] = {'A','B'}; // there is loss of at the end of string
?
在执行strcpy前和strcpy后分别显示这些字符串的内存地址和字符串的内容,结果如下:
?
addr: 0xbfb08a39 | s0 : string
addr: 0xbfb08a41 | s1 : str-1
addr: 0xbfb08a51 | s2 : str-2and123
addr: 0xbfb08a49 | s3 : AB
addr: 0xbfb08a39 | s4 : ABstring
?----- after running strcpy(s1,s3) -----
addr: 0xbfb08a39 | s0 : string
addr: 0xbfb08a41 | s1 : AB
addr: 0xbfb08a51 | s2 : str-2and123
addr: 0xbfb08a49 | s3 : AB
addr: 0xbfb08a39 | s4 : ABstring
?----- after running strcpy(s1,s2) -----
addr: 0xbfb08a39 | s0 : string
addr: 0xbfb08a41 | s1 : str-2and123
addr: 0xbfb08a51 | s2 : str-2and123
addr: 0xbfb08a49 | s3 : 123
addr: 0xbfb08a39 | s4 : ABstring
?
从运行结果中,大家可以看到当使用strcpy(s1,s3)把s3的内容复制到s1中时,s1的内容变成了s3的
内容,其它几个字符串的内容没有改变。
?
当使用strcpy(s1,s2)把s2的内容复制到s1中时,s1的内容变成了s2的内容,而且s3的内容也被改变了。
我们再看看s1和s3的地址相邻很近,肯定是在复制过程中把s1后面的内存给“污染”了,所以s3的内容
被修改了,s3就这样中了躺枪。为了大家更加清楚地理解s3是如何中躺枪的,我们对内存中的地址进
行分析:
?
内存中的地址:0xbfb08a4 1 2 3 4 5 6 7 8 9 a b c d e f
地址中的数值: s t r - 2 a n d 1 2 3
?
大家看看s1的地址从0xbfb08a41开始,占用8个字节,也就是到0xbfb08a48结束,但是执行strcpy操
作时复制了11个字符,这显然超过了s1占有的8个字节,因此它把后面3个字节的内容也强制占用了,但是
后面3个字节的空间是系统分配给s3的,s3虽然拥有这3个字节的空间,但是里面的内容已经被s1强制占用
时修改了。s3就这样中了躺枪。
?
strncpy函数原型:char * strcpy(char *s1, const char *s2,size_t n)strncpy函数用法:它把s2中的n个字符复制到s1中,并且返回s1.strncpy注意事项:如果s2中的字符数量大于n,那么只复制n个字符到s1中,而且不会在n个字符后面加上字符串的小尾巴。如果s2中的字符数量小于n,那么只复制s2中所有的字符,不足的用空字符:进行补充,直到n个字符为止。我们一般的经验是,在复制时设置n的值比s1的容量小于1,并且手动给s1加上小尾巴。如果n的值大于s1的容量就和strcpy一样了。引入strncpy就是为了通过n来控制复制的内容,避免类似strcpy复制时的缺陷。因此,我们要用好n这张牌。关键时候来个一招招制胜,哈哈。
?
我们举一个实际的例子来说明,程序中还使用刚才定义的字符串,在执行strncpy前和strncpy后分别显
示这些字符串的内存地址和字符串的内容,结果如下:
?
addr: 0xbfed56a9 | s0 : string //执行strncpy前各个字符串的值
addr: 0xbfed56b1 | s1 : str-1
addr: 0xbfed56c1 | s2 : str-2and123
addr: 0xbfed56b9 | s3 : AB
addr: 0xbfed56a9 | s4 : ABstring
----- after running strncpy(s1,s3,SIZE) -----
addr: 0xbfed56a9 | s0 : string
addr: 0xbfed56b1 | s1 : AB //s3中字符的数量小于SIZE,所以有小尾巴
addr: 0xbfed56c1 | s2 : str-2and123
addr: 0xbfed56b9 | s3 : AB
addr: 0xbfed56a9 | s4 : ABstring
----- after running strncpy(s1,s2,SIZE) -----
addr: 0xbfed56a9 | s0 : string
addr: 0xbfed56b1 | s1 : str-2andAB //从s2中复制了8个字符,但是没有小尾巴
addr: 0xbfed56c1 | s2 : str-2and123
addr: 0xbfed56b9 | s3 : AB
addr: 0xbfed56a9 | s4 : ABstring
----- after running strncpy(s1,s2,SIZE-1) -----
addr: 0xbfed56a9 | s0 : string
addr: 0xbfed56b1 | s1 : str-2an //从s2中复制了7个字符,手动加上小尾巴
addr: 0xbfed56c1 | s2 : str-2and123
addr: 0xbfed56b9 | s3 : AB
addr: 0xbfed56a9 | s4 : ABstring
----- after running strncpy(s1,s2,SIZE+3) -----
addr: 0xbfed56a9 | s0 : string
addr: 0xbfed56b1 | s1 : str-2and123 //复制的字符数量大于s1的容量,效果等于strcpy
addr: 0xbfed56c1 | s2 : str-2and123
addr: 0xbfed56b9 | s3 : 123
addr: 0xbfed56a9 | s4 : ABstring
?
我们使用strncpy(s1,s2,n)进行了四次复制操作:
第一次