码海拾遗:strcpy()、strncpy()和strcpy_s()区别
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 }
2、strncpy()
原型:char *strncpy(char *dst,const char *src,size_t len)
功能:从以src为首地址的字符串中之多复制len个字符到以dst为首地址的字符串。如果在[0,len]之间没有'\0'结束符,则dst中没有结束符。
如果len大于src的长度,则dst中多余位置自动置为null
测试代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 int main(int argc,char* argv[]) 5 { 6 char buf[20]; 7 char *str = "hello world"; 8 9 strncpy(buf,str,20); 10 printf("buf:%s\nsizeof(buf) = %ld\nstrlen(buf) = %ld\n", 11 buf,sizeof(buf),strlen(buf)); 12 13 return 0; 14 }
如果len小于src的长度,则dst中没有结束符,dst输出时会出现乱码,直至碰到'\0'结束符为止。
测试代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 int main(int argc,char* argv[]) 5 { 6 char buf[4]; 7 char *str = "hello world"; 8 9 strncpy(buf,str,5); 10 printf("buf:%s\nsizeof(buf) = %ld\nstrlen(buf) = %ld\n", 11 buf,sizeof(buf),strlen(buf)); 12 13 return 0; 14 }
3、strcpy_s()
该函数是VS2005之后的VS提供的,并非C标准函数
原型:strcpy_s( char *dst, size_t num, const char *src )
功能:同strcpy()函数功能相同,不同之处在于参数中多了个size_t类型的参数,该参数为字符串dst的长度,当存在缓存区溢出的问题时(即src的长度大于dst的长度),strcpy_s()会抛出异常;而strcpy()结果则未定,因为它错误地改变了程序中其他部分的内存的数据,可能不会抛出异常但导致程序数据错误,也可能由于非法内存访问抛出异常。