这个函数用两个参数、三个参数都可以,只要可以保证缓冲区大小。
使用三个参数时的代码如下:
errno_t strcpy_s(char *strDestination,size_t numberOfElements, const char *strSource);
使用三个参数时代码如下
errno_t strcpy_s(char (&strDestination)[size],const char *strSource); // C++ only
例子:
#include<iostream>
#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];会出错:提示不支持两个参数
cout<<"strlen(str1):"<<strlen(str1)<<endl<<"strlen(str):"<<strlen(str)<<endl;
printf(str1);
printf(" ");
cout<<str<<endl;
}
int main()
{
Test();
return 0;
}
#include<iostream>
#include<string.h>
using name space std;
void Test(void ) {
char *str1=NULL;
str1=newchar[20];
char str[7];
strcpy_s(str1,20,"hello world");//三个参数
strcpy_s(str,"hello");//两个参数但如果:char *str=new char[7];会出错:提示不支持两个参数
cout<<"strlen(str1):"<<strlen(str1)<<endl<<"strlen(str):"<<strlen(str)<<endl; printf(str1); printf(" ");
cout<<str<<endl;
}
int main()
#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];会出错:提示不支持两个参数
cout<<"strlen(str1):"<<strlen(str1)<<endl<<"strlen(str):"<<strlen(str)<<endl;
printf(str1);
printf(" ");
cout<<str<<endl;
}
int main()
{
Test();
return 0;
}
#include<iostream>
#include<string.h>
using name space std;
void Test(void ) {
char *str1=NULL;
str1=newchar[20];
char str[7];
strcpy_s(str1,20,"hello world");//三个参数
strcpy_s(str,"hello");//两个参数但如果:char *str=new char[7];会出错:提示不支持两个参数
cout<<"strlen(str1):"<<strlen(str1)<<endl<<"strlen(str):"<<strlen(str)<<endl; printf(str1); printf(" ");
cout<<str<<endl;
}
int main()
{ Test(); return 0; }
输出为:
strlen(str1): 11 //另外要注意:strlen(str1)是计算字符串的长度,不包括字符串末尾的“