指针与数组的区别
2013-03-29 08:33
225 查看
char *buf1="hello";
char *buf2="hello";
if(buf1==buf2)
cout<<"equal"<<endl;
else
cout<<"unequal"<<endl;
2.
char buf1[10]="hello";
char buf2[10]="hello";
if(buf1==buf2)
cout<<"equal"<<endl;
else
cout<<"unequal"<<endl;
指针是常量,不能改变字符串中字符的值
char *p="abc";
p="cde";
// p[1]='m'; 这句编译可以通过,但是运行就出错了
cout<<"p="<<p<<endl; 打印出cde
cout<<"p[1]="<<p[1]<<endl; 打印出d
答案:输出分别为equal unequal.
char *buf2="hello";
if(buf1==buf2)
cout<<"equal"<<endl;
else
cout<<"unequal"<<endl;
2.
char buf1[10]="hello";
char buf2[10]="hello";
if(buf1==buf2)
cout<<"equal"<<endl;
else
cout<<"unequal"<<endl;
指针是常量,不能改变字符串中字符的值
char *p="abc";
p="cde";
// p[1]='m'; 这句编译可以通过,但是运行就出错了
cout<<"p="<<p<<endl; 打印出cde
cout<<"p[1]="<<p[1]<<endl; 打印出d
答案:输出分别为equal unequal.