C二级 函数问题

来源:百度知道 编辑:UC知道 时间:2024/05/27 18:52:27
#include<stdio.h>
#include<string.h>
void fun(char *str,char ch)
{
while(*str&&*str!=ch)
str++;
if(*str!=ch)
{
str[0]=ch;
str[1]=0;
}
}
main()
{
char s[81],c;
printf("\nPlease enter a string:\n");
gets(s);
printf("\nPlease enter the character to search:");
c=getchar();
fun(s,c);
printf("\nThe result is %s\n",s);
getch();
}

麻烦大家帮我看下 fun这个函数 中的
str[0]=ch;
str[1]=0;
ch为什么会被赋值到 最后一位

while(*str&&*str!=ch)
str++;
当执行完上面语句时,若字符串中没有所要查找的字符,str已经指向了最后一位,即指向空字符那一位'\0'
str[0]=ch; 相对地址为0,即str现所指向的地址
str[1]=0; 字符串结束字符

while(*str&&*str!=ch)
str++;
最后str是等于s【81】的地址了 当然赋值在最后以为了啊!