轉自:
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("\n"); }