i am doing strcpy from a larger string into a relatively smaller string using two ways.According to me both the methods allocate space statically(as i am not using any malloc or calloc in either of my cases). but still method1 gives me segementation fault(in ubuntu) or just cracshes the exe generated (in windows),i.e i am trying to access area out of my way, while method2 doesn't . why?
method 1:
char *a = "hi";
char *b = "hisi";
try
{
strcpy(a, b);
cout << "all ok";
}
catch (exception e)
{
cout << "error";
}
method2:
char a[3] = "hi";
char b[5] = "hisi";
try
{
strcpy(a, b);
cout << "all ok";
}
catch (exception e)
{
cout << "error";
}