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"
当我的程序运行时,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;
}
1 个解决方案
#1
2
If you want to interpret ch as a string, you should terminate the array with '/0'. Replace
如果要将ch解释为字符串,则应使用“/ 0”终止数组。更换
char ch[] = { 'x','y','z'}
with
同
char ch[] = { 'x','y','z', '\0' }
and the output becomes "Array is xyz."
输出变为“Array is xyz”。
For more information, read https://en.wikipedia.org/wiki/Null-terminated_string
有关更多信息,请阅读https://en.wikipedia.org/wiki/Null-terminated_string
#1
2
If you want to interpret ch as a string, you should terminate the array with '/0'. Replace
如果要将ch解释为字符串,则应使用“/ 0”终止数组。更换
char ch[] = { 'x','y','z'}
with
同
char ch[] = { 'x','y','z', '\0' }
and the output becomes "Array is xyz."
输出变为“Array is xyz”。
For more information, read https://en.wikipedia.org/wiki/Null-terminated_string
有关更多信息,请阅读https://en.wikipedia.org/wiki/Null-terminated_string