1、strcpy()
原型:char *strcpy(char *dst,const char *src)
功能:将以src为首地址的字符串复制到以dst为首地址的字符串,包括'\0'结束符,返回dst地址。要求:src和dst所指内存区域不可以重叠且dst必须有足够的空间来容纳src的字符串,若dst空间不足,编译时并不会报错,但执行时因系统不同会出现不同的结果:Mac系统提示“Abort trap:6”(Mac);CentOS7系统会正常运行(可能是个例,可以正常运行)
测试代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 int main(int argc,char* argv[]) 5 { 6 char buf[2]; 7 char *str = "hello world"; 8 9 strcpy(buf,str); 10 printf("buf:%s\nsizeof(buf) = %ld\nstrlen(buf) = %ld\n", 11 buf,sizeof(buf),strlen(buf)); 12 13 return 0; 14 }