简体   繁体   中英

strcpy_s for char** and char[][]

I used strcpy_s as below:

char names[2][20];
strcpy_s(names[0],"Michael");
strcpy_s(names[1],"Danny");

and it worked all right.

But when I changed to char ** ,

int size1=2;
int size2=20;

char **names=new char*[size1];
for(int i=0;i<size1;i++)
  names[i]=new char[size2];
strcpy_s(names[0],"Michael");
strcpy_s(names[1],"Danny");

It gives me this error message:

error C2660: 'strcpy_s' : function does not take 2 arguments

Why is this happening? I need to dynamically create char arrays, so what should I do?

There are two forms of strcpy_s (at least on Windows): one for pointers and one for arrays.

errno_t strcpy_s(
   char *strDestination,
   size_t numberOfElements,
   const char *strSource 
);
template <size_t size>
errno_t strcpy_s(
   char (&strDestination)[size],
   const char *strSource 
); // C++ only

When using pointers you have to specify the number of elements of the destination buffer:

strcpy_s(names[0], size2, "Michael");
strcpy_s(names[1], size2, "Danny");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM