When my program runs, the IF ( ch[0] == 'P') actually puts the value 'P' into ch[0]. Any ideas what is happening here? The output is: "Array is Pyz"
char *try1(char ch[]);
int main()
{
char ch[] = { 'x','y','z' }, *ch1;
ch1=try1(ch);
printf("\nArray is %s\n",ch1);
return 0;
}
char *try1 (char ch[])
{
if (ch[0]=='P')
{
ch[1]='Q';
}
return ch;
}
==and not a=?ch[]is not NUL terminated, so the call toprintf()will result in garbage being output after the actual 3 bytes in the array until a NUL byte is encountered. This is undefined behavior and can lead to a seg fault event.