既然字符串常量,为什么赋给char*而不是const char*

a2471388918 2013-08-31 11:23:18
既然是字符串常量,为什么赋给char*而不是const char*,因为通过指针变量来改变字符串常量是不行的,那为什么还可以赋值给char*而不是const char*;就像const int a=5;int *p=&a;这样就会出错,为什么那个可以。
...全文
822 30 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
a2471388918 2013-09-20
  • 打赏
  • 举报
回复
引用 4 楼 baihacker 的回复:
[quote=引用 3 楼 a2471388918 的回复:] [quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
那堆英文是什么[/quote] 就是说大概可以把"123"的当一个char* p;声明的变量p使。[/quote] 刚刚一个不小心,真的不小心,看到了你的一条回复,原来你看过那么多的书的啊!那你是不是很强了,还有就是,怎么那么多的时间的啊
adamsun 2013-09-04
  • 打赏
  • 举报
回复
TMD,国内C++铺天盖地火起来是2000年左右,都十来年了,还有人出这些书瞎搞,学C++起步就一本书,《C++ primer》,版本越新越好,他讲的很细,也很到位,其他都不用看,把这本书吃透了就可以了。接下来就一本《Effective C++》,Scott Meyer写的,找侯捷的译本。就这两本书,你要能吃透,C++你可拍胸脯了。至于其他好书有好多,可以边工作边选择性的深入了。至于什么模板,什么meta programming,boost库都要建立在你把前2本书吃透的基础上,没有那个基础,其他都是白搭。现实中搞开发的,除非微软研究院这种研发创新一体的公司,大部分公司,2本书足够你混一辈子了。 我要说的是你得把算法和C语言学好了,学到精深,非你莫属。再结合做那一块业务的,把那块业务搞透,你是个人才了!
a2471388918 2013-09-04
  • 打赏
  • 举报
回复
引用 27 楼 ri_aje 的回复:
[quote=引用 26 楼 a2471388918 的回复:] [quote=引用 25 楼 akirya 的回复:] [quote=引用 23 楼 a2471388918 的回复:] [quote=引用 22 楼 ri_aje 的回复:] [quote=引用 19 楼 a2471388918 的回复:] [quote=引用 17 楼 ri_aje 的回复:] [quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
引用 2 楼 akirya 的回复:
历史原因,造成有这么一个特殊的规定。
c++11 已经取消了这条规定。[/quote] 你看我最新的那个帖子,我附一段给你看; const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 这里的clone函数返回的是一个const引用,再用sysop引用它的时候,为什么不是 const sysop &jol=clone(sysopr); 而这里把那个const省略了也行? 但是我自己测试一个:看注释,是我的问题
#include <iostream>

using namespace std;
struct student{
char *s;
char a[5];}st1;
void show(student &st);
const int change(int &k);
int main(){
	student ss={"nihao ","o o "};
cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
cout<<endl;
student st=ss;
show(ss);cout<<endl;
show(st);
cout<<endl;
int i=5;
int &j=change(i);//这里如果没有const在前面就会出错,但是为什么书本上的那个例子
//(也就是上面那个,不是我这个的)没有const
cout<<endl<<j;
return 0;}
void show(student &ss){
	cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
}
const int change(int &k){
	int *p=new int ;
	*p=k;
	return *p;
}
[/quote] (1) 你抄错了。 (2) 书太烂了。[/quote] 书本的是这个const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 我没有抄错; 以及那本书是c++primer plus正版。[/quote] 《c++primer plus》确实是本烂书 好书是《C++Primer》[/quote] 啊,我好喜欢这本书的啊,你的意思是说书的这里是错的??[/quote] 你自己找个比较新的编译器试试呗,把原书的程序照抄,看看能不能编译。[/quote] 我在6.0中试了我的那段是错的(书本就那一段例子,是说明引用的返回要在函数结束后存在)
ri_aje 2013-09-03
  • 打赏
  • 举报
回复
引用 26 楼 a2471388918 的回复:
[quote=引用 25 楼 akirya 的回复:] [quote=引用 23 楼 a2471388918 的回复:] [quote=引用 22 楼 ri_aje 的回复:] [quote=引用 19 楼 a2471388918 的回复:] [quote=引用 17 楼 ri_aje 的回复:] [quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
引用 2 楼 akirya 的回复:
历史原因,造成有这么一个特殊的规定。
c++11 已经取消了这条规定。[/quote] 你看我最新的那个帖子,我附一段给你看; const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 这里的clone函数返回的是一个const引用,再用sysop引用它的时候,为什么不是 const sysop &jol=clone(sysopr); 而这里把那个const省略了也行? 但是我自己测试一个:看注释,是我的问题
#include <iostream>

using namespace std;
struct student{
char *s;
char a[5];}st1;
void show(student &st);
const int change(int &k);
int main(){
	student ss={"nihao ","o o "};
cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
cout<<endl;
student st=ss;
show(ss);cout<<endl;
show(st);
cout<<endl;
int i=5;
int &j=change(i);//这里如果没有const在前面就会出错,但是为什么书本上的那个例子
//(也就是上面那个,不是我这个的)没有const
cout<<endl<<j;
return 0;}
void show(student &ss){
	cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
}
const int change(int &k){
	int *p=new int ;
	*p=k;
	return *p;
}
[/quote] (1) 你抄错了。 (2) 书太烂了。[/quote] 书本的是这个const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 我没有抄错; 以及那本书是c++primer plus正版。[/quote] 《c++primer plus》确实是本烂书 好书是《C++Primer》[/quote] 啊,我好喜欢这本书的啊,你的意思是说书的这里是错的??[/quote] 你自己找个比较新的编译器试试呗,把原书的程序照抄,看看能不能编译。
a2471388918 2013-09-03
  • 打赏
  • 举报
回复
引用 25 楼 akirya 的回复:
[quote=引用 23 楼 a2471388918 的回复:] [quote=引用 22 楼 ri_aje 的回复:] [quote=引用 19 楼 a2471388918 的回复:] [quote=引用 17 楼 ri_aje 的回复:] [quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
引用 2 楼 akirya 的回复:
历史原因,造成有这么一个特殊的规定。
c++11 已经取消了这条规定。[/quote] 你看我最新的那个帖子,我附一段给你看; const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 这里的clone函数返回的是一个const引用,再用sysop引用它的时候,为什么不是 const sysop &jol=clone(sysopr); 而这里把那个const省略了也行? 但是我自己测试一个:看注释,是我的问题
#include <iostream>

using namespace std;
struct student{
char *s;
char a[5];}st1;
void show(student &st);
const int change(int &k);
int main(){
	student ss={"nihao ","o o "};
cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
cout<<endl;
student st=ss;
show(ss);cout<<endl;
show(st);
cout<<endl;
int i=5;
int &j=change(i);//这里如果没有const在前面就会出错,但是为什么书本上的那个例子
//(也就是上面那个,不是我这个的)没有const
cout<<endl<<j;
return 0;}
void show(student &ss){
	cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
}
const int change(int &k){
	int *p=new int ;
	*p=k;
	return *p;
}
[/quote] (1) 你抄错了。 (2) 书太烂了。[/quote] 书本的是这个const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 我没有抄错; 以及那本书是c++primer plus正版。[/quote] 《c++primer plus》确实是本烂书 好书是《C++Primer》[/quote] 啊,我好喜欢这本书的啊,你的意思是说书的这里是错的??
  • 打赏
  • 举报
回复
引用 23 楼 a2471388918 的回复:
[quote=引用 22 楼 ri_aje 的回复:] [quote=引用 19 楼 a2471388918 的回复:] [quote=引用 17 楼 ri_aje 的回复:] [quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
引用 2 楼 akirya 的回复:
历史原因,造成有这么一个特殊的规定。
c++11 已经取消了这条规定。[/quote] 你看我最新的那个帖子,我附一段给你看; const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 这里的clone函数返回的是一个const引用,再用sysop引用它的时候,为什么不是 const sysop &jol=clone(sysopr); 而这里把那个const省略了也行? 但是我自己测试一个:看注释,是我的问题
#include <iostream>

using namespace std;
struct student{
char *s;
char a[5];}st1;
void show(student &st);
const int change(int &k);
int main(){
	student ss={"nihao ","o o "};
cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
cout<<endl;
student st=ss;
show(ss);cout<<endl;
show(st);
cout<<endl;
int i=5;
int &j=change(i);//这里如果没有const在前面就会出错,但是为什么书本上的那个例子
//(也就是上面那个,不是我这个的)没有const
cout<<endl<<j;
return 0;}
void show(student &ss){
	cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
}
const int change(int &k){
	int *p=new int ;
	*p=k;
	return *p;
}
[/quote] (1) 你抄错了。 (2) 书太烂了。[/quote] 书本的是这个const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 我没有抄错; 以及那本书是c++primer plus正版。[/quote] 《c++primer plus》确实是本烂书 好书是《C++Primer》
a2471388918 2013-09-02
  • 打赏
  • 举报
回复
引用 20 楼 Saleayas 的回复:
我从来都不会这样写,习惯了。
你不会怎样写,你写的加不加const
a2471388918 2013-09-02
  • 打赏
  • 举报
回复
引用 22 楼 ri_aje 的回复:
[quote=引用 19 楼 a2471388918 的回复:] [quote=引用 17 楼 ri_aje 的回复:] [quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
引用 2 楼 akirya 的回复:
历史原因,造成有这么一个特殊的规定。
c++11 已经取消了这条规定。[/quote] 你看我最新的那个帖子,我附一段给你看; const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 这里的clone函数返回的是一个const引用,再用sysop引用它的时候,为什么不是 const sysop &jol=clone(sysopr); 而这里把那个const省略了也行? 但是我自己测试一个:看注释,是我的问题
#include <iostream>

using namespace std;
struct student{
char *s;
char a[5];}st1;
void show(student &st);
const int change(int &k);
int main(){
	student ss={"nihao ","o o "};
cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
cout<<endl;
student st=ss;
show(ss);cout<<endl;
show(st);
cout<<endl;
int i=5;
int &j=change(i);//这里如果没有const在前面就会出错,但是为什么书本上的那个例子
//(也就是上面那个,不是我这个的)没有const
cout<<endl<<j;
return 0;}
void show(student &ss){
	cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
}
const int change(int &k){
	int *p=new int ;
	*p=k;
	return *p;
}
[/quote] (1) 你抄错了。 (2) 书太烂了。[/quote] 书本的是这个const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 我没有抄错; 以及那本书是c++primer plus正版。
ri_aje 2013-09-02
  • 打赏
  • 举报
回复
引用 19 楼 a2471388918 的回复:
[quote=引用 17 楼 ri_aje 的回复:] [quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
引用 2 楼 akirya 的回复:
历史原因,造成有这么一个特殊的规定。
c++11 已经取消了这条规定。[/quote] 你看我最新的那个帖子,我附一段给你看; const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 这里的clone函数返回的是一个const引用,再用sysop引用它的时候,为什么不是 const sysop &jol=clone(sysopr); 而这里把那个const省略了也行? 但是我自己测试一个:看注释,是我的问题
#include <iostream>

using namespace std;
struct student{
char *s;
char a[5];}st1;
void show(student &st);
const int change(int &k);
int main(){
	student ss={"nihao ","o o "};
cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
cout<<endl;
student st=ss;
show(ss);cout<<endl;
show(st);
cout<<endl;
int i=5;
int &j=change(i);//这里如果没有const在前面就会出错,但是为什么书本上的那个例子
//(也就是上面那个,不是我这个的)没有const
cout<<endl<<j;
return 0;}
void show(student &ss){
	cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
}
const int change(int &k){
	int *p=new int ;
	*p=k;
	return *p;
}
[/quote] (1) 你抄错了。 (2) 书太烂了。
赵4老师 2013-09-02
  • 打赏
  • 举报
回复
不要纠结各种常量了,这个世界上唯一不变的就是变化。用API WriteProcessMemory还能修改正运行的其它进程的内存里面的所谓常量呢!
todd_leftcode 2013-09-02
  • 打赏
  • 举报
回复
楼主,你看的到底啥书啊?
ri_aje 2013-09-02
  • 打赏
  • 举报
回复
引用 1 楼 baihacker 的回复:
A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
引用 2 楼 akirya 的回复:
历史原因,造成有这么一个特殊的规定。
c++11 已经取消了这条规定。
Saleayas 2013-09-02
  • 打赏
  • 举报
回复
我从来都不会这样写,习惯了。
a2471388918 2013-09-02
  • 打赏
  • 举报
回复
引用 17 楼 ri_aje 的回复:
[quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
引用 2 楼 akirya 的回复:
历史原因,造成有这么一个特殊的规定。
c++11 已经取消了这条规定。[/quote] 你看我最新的那个帖子,我附一段给你看; const sysop& clone (sysop& sysopref){ sysop *psysop=new sysop; *psysop=sysopref; return *psysop; } ........ ........ sysop &jol=clone(sysopr); 这里的clone函数返回的是一个const引用,再用sysop引用它的时候,为什么不是 const sysop &jol=clone(sysopr); 而这里把那个const省略了也行? 但是我自己测试一个:看注释,是我的问题
#include <iostream>

using namespace std;
struct student{
char *s;
char a[5];}st1;
void show(student &st);
const int change(int &k);
int main(){
	student ss={"nihao ","o o "};
cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
cout<<endl;
student st=ss;
show(ss);cout<<endl;
show(st);
cout<<endl;
int i=5;
int &j=change(i);//这里如果没有const在前面就会出错,但是为什么书本上的那个例子
//(也就是上面那个,不是我这个的)没有const
cout<<endl<<j;
return 0;}
void show(student &ss){
	cout<<ss.s;
cout<<(int*) ss.s;
cout<<"  "<<ss.a;
cout<<" ";
cout<<(int *)ss.a;
}
const int change(int &k){
	int *p=new int ;
	*p=k;
	return *p;
}
a2471388918 2013-09-01
  • 打赏
  • 举报
回复
引用 4 楼 baihacker 的回复:
[quote=引用 3 楼 a2471388918 的回复:] [quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
那堆英文是什么[/quote] 就是说大概可以把"123"的当一个char* p;声明的变量p使。[/quote] #include <iostream> #include <cstring > using namespace std; int main(){ char a[5]="sdfd"; char ch='a'; cout<<"ch_addre: "<<(int *)ch<<endl; if (strcmp(a,&ch)>0) cout<<"o o "; //正确 if (strcmp(a,(int*)ch)>0) cout<<"o o ";//报错 cout<<endl; return 0;} 这个你怎么看,虽然正确的那个是把ch和之后未知的字符与a作比较,但是同样是地址,为什么报错的那个就不行
baihacker 2013-09-01
  • 打赏
  • 举报
回复
引用 3 楼 a2471388918 的回复:
[quote=引用 1 楼 baihacker 的回复:] A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. 有这样一条规定,不过不建议用。
那堆英文是什么[/quote] 就是说大概可以把"123"的当一个char* p;声明的变量p使。
a2471388918 2013-09-01
  • 打赏
  • 举报
回复
引用 15 楼 todd_leftcode 的回复:
主流编译器大多已经修正这一做法, 现在 char* p = "this is a test"; 通常会有警告或错误。 正确的方法是 const char * p = "this is a test"; 楼上的 "int*是对字符串取地址的意思,就像cout<<(int *)a;就是输出其字符串" 老实说我没太理解。 这个看上去只是把变量a强制转换成 int 指针而已。像6#把个字符变量强制转换成int指针然后又试图将其当成 const char* 传给strcmp, 不出错对不住小伙伴吧?
不是,书本上的是说对一个字符串取地址,是在前面用int*,我说错了,是输出其字符串的地址,不是字符串,那同样是地址,虽然类型怪怪的,但是,。。
todd_leftcode 2013-09-01
  • 打赏
  • 举报
回复
主流编译器大多已经修正这一做法, 现在 char* p = "this is a test"; 通常会有警告或错误。 正确的方法是 const char * p = "this is a test"; 楼上的 "int*是对字符串取地址的意思,就像cout<<(int *)a;就是输出其字符串" 老实说我没太理解。 这个看上去只是把变量a强制转换成 int 指针而已。像6#把个字符变量强制转换成int指针然后又试图将其当成 const char* 传给strcmp, 不出错对不住小伙伴吧?
a2471388918 2013-09-01
  • 打赏
  • 举报
回复
引用 10 楼 baihacker 的回复:
给定函数,有一堆形参,在调用时要求实参和形参的类型按一定规则匹配,如此而已。 和地址扯不上关系。
int*是对字符串取地址的意思,就像cout<<(int *)a;就是输出其字符串的地址
a2471388918 2013-09-01
  • 打赏
  • 举报
回复
引用 5 楼 mougaidong 的回复:
http://blog.csdn.net/mougaidong/article/details/6372765
http://bbs.csdn.net/topics/390576135看我这个怎么理解
加载更多回复(10)

65,198

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧