strcpy_s和strcpy()

转自:

https://www.cnblogs.com/hrhguanli/p/4570093.html

strcpy_s和strcpy()函数功能几乎相同。strcpy函数。就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它仅仅能假定缓冲足够大来容纳要拷贝的字符串。

在程序执行时,这将导致不可预料的行为。

用strcpy_s就能够避免这些不可预料的行为。

这个函数用两个參数、三个參数都能够,仅仅要能够保证缓冲区大小。

 

#include<cstring>
using namespace std;

void Test(void)
{
    char *str1 = NULL;
    str1 = new char[20];
    char str[7];

    strcpy_s(str1, 20, "hello world");//三个參数
    strcpy_s(str, "hello");//两个參数但假设:char *str=new char[7];会出错:提示不支持两个參数

    printf(str);

    printf("
");
}
原文地址:https://www.cnblogs.com/zhangxuan/p/10757847.html