当我的程序运行时,IF(ch [0] ==' P')实际上将值设为' P'进入ch [0]。有什么想法在这里发生了什么?输出是:"数组是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;
}
答案 0 :(得分:2)
如果要将ch解释为字符串,则应使用“/ 0”终止数组。取代
char ch[] = { 'x','y','z'}
与
char ch[] = { 'x','y','z', '\0' }
,输出变为“Array is xyz。”
有关详情,请参阅https://en.wikipedia.org/wiki/Null-terminated_string