1

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?

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Michael
  • 673
  • 2
  • 5
  • 23

1 Answers1

9

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");
Pubby
  • 51,882
  • 13
  • 139
  • 180
  • +1 was unaware of the template version (even though it was on page I linked in my deleted answer!) – hmjd Jun 03 '12 at 09:28
  • @Michael No problem. Don't forget to click the green check mark to accept the answer as correct. – Pubby Jun 03 '12 at 12:56