Program 1:
void function(char arr[])
{
arr[0] = 'X';
printf("%s",arr);
}
int main()
{
function("MyString");
}
Output: Segmentation fault
Program 2:
int main()
{
char arr[] = "MyString";
arr[0] = 'X';
printf("%s",arr);
}
Output: XyString
What is the difference between program 1 and program 2? In prog1 also string value (not by reference) is passed to array so it should copy to array and allow to modify it? But it is throwing segmentation fault. In prog2 successfully allowing to change the arr[]. Why in prog1 it is not working?