请教一次C++问题

来源:百度知道 编辑:UC知道 时间:2024/09/21 05:28:52
#include<iostream>
#include<cstring>
using namespace std;
void main()
{char str[]="very good!",*ps="thank you!",string[10];
strcpy(string,str);
cout<<"before change:"<<endl;
cout<<"the string str is:"<<str<<endl;
cout<<"the string ps is:"<<ps<<endl;
cout<<"after change:"<<endl;
strcpy(str,ps);
strcpy(ps,string);
cout<<"the change's str is:"<<str<<endl;
cout<<"the change's ps is:"<<ps<<endl;
}为什么运行是提示有错误呀?

数组越界....ps[15]="thank you!",就好....

strcpy(ps,string);
ps是一个常量指针,不允许修改它指向的内容。


*ps="thank you!"

修改为
ps[]="thank you!"

即可

好象是因为strcpy()的第一个参数必须是数组名,不能是其它类型。C语言书上说“strcpy(字符数组1,字符串2)‘字符数组1’必须写成数组名形式,‘字符串2’可以是字符数组名,也可以是一个字符串常量。”————C程序设计(第三版)148页