strcpy()与strcpy_s()的区别

文章目录

1. strcpy

C语言标准库函数,包含头文件:#include<stdio.h> #include<string.h>

实现:

char *strcpy(char *des, const char *source)
{
char *r = des;
assert((des!=NULL) && source!=NULL);
while((*r++ = *source++) != ‘\0’); //赋值表达式返回左操作数,所以在’\0’后,循环停止
return des’;
}
使用时注意:若数组长度不足以容纳整个字符串,则会导致程序出现不可预知的错误。

2.strcpy_s

与strcpy的功能几乎是一样的。

区别:strcpy无法保证有效的缓冲区尺寸,只能确保使用了足够大的缓冲区,而strcpy_s则可以避免以上行为。

有两个参数和三个参数两种类型。

三个参数时:

errno_t strcpy_s(char *strDestination, size_t numberofElements, const char *strSource){}

两个参数时:

errno_t strcpy_s(char (&strDestination)[size], const char *strSource){}//C++ only

发布了31 篇原创文章 · 获赞 12 · 访问量 2762

猜你喜欢

转载自blog.csdn.net/weixin_42877426/article/details/104321554