I have the following code, I wonder what is the difference between the implementation of str2
and str3
, they both give the same results, which one is more prone to errors?
EDIT: when I was testing the representation of str2
, I have found that one time my code crashed because str2
was a bad pointer!
/* strcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[] = "Sample string";
char str0[] = "Sample String and more";
char* str2;
str2 = new char[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,str1);
strcpy (str2,str0);// crash happened here str2 is bad pointer!!!
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
delete str2;
return 0;
}