C++指针指向字符串

#include<iostream>
using namespace std;
#include<string.h>
bool is_huiwen(char a[],int length)
{
const char *src=a;
const char *end;//这句中 为什么要加const呢
end=src+length-1;
while(src<end)
{ if(*src==*end)
{ src++;end--;}
else return false;
}
return true;
}
int main()
{ int len;
char c[10];
cout<<"enter:"<<endl;
cin>>c;
len=strlen(c);
bool h;
h=is_huiwen(c,len);
if(h) cout<<"hui_wen"<<endl;
else cout<<"non_hui_wen"<<endl;
return 0;
}

#include<iostream>
using namespace std;
#include<string.h>
bool is_huiwen(char a[],int length)
{
const char *src=a;
const char *end;//这句中 为什么要加const呢
end=src+length-1;
while(src<end)
{ if(*src==*end)
{ src++;end--;}
else return false;
}
return true;
}
int main()
{ int len;
char c[10];
cout<<"enter:"<<endl;
cin>>c;
len=strlen(c);
bool h;
h=is_huiwen(c,len);
if(h) cout<<"hui_wen"<<endl;
else cout<<"non_hui_wen"<<endl;
return 0;
}


只有一个const,如果const位于*左侧,表示指针所指数据是常量,不能通过解引用修改该数据;指针本身是变量,可以指向其他的内存单元。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-08-30
end指向的内容不可更改。
第2个回答  2020-12-25

第五十六集 指针指向字符串

相似回答