char srch(char x[],char k){
int i=0;
while(x[i]!='\0')
{
if(k==x[i])
{
const char *h=&(x[i]);
const void *l = h;
cout<<"\n\nTHE CHARACTER "<<x[i]<<" FOUND AT ADDRESS "<<l<<"\n\n\n";
exit(0);
}
i++;
}
return NULL;
}
int main(){
system("cls");
char str[20],ch;
cout<<"ENTER THE STRING:-\n";
gets(str);
cout<<"ENTER THE CHARACTER WHICH YOU WANT TO SEEK:-\n";
cin>>ch;
srch(str,ch);
if(NULL);
cout<<"\n\nCHARACTER NOT FOUND ";}
现在,如果我的字符串是“tarun”并且我要寻找的角色是“a”,那么它完美地显示了“a”的地址。但是如果我用这个替换我的 srch 函数:-
char srch(char x[],char k){
int i=0;
while(x[i]!='\0')
{
if(k==x[i])
{
const char *h=&(x[i]);
const char *l = h;
cout<<"\n\nTHE CHARACTER "<<x[i]<<" FOUND AT ADDRESS "<<l<<"\n\n\n";
exit(0);
}
i++;
}
return NULL;}
然后不是地址,而是显示arun。为什么当我使用 char* 而不是 void* 时会发生这种情况?