看看那里错了

coolajie 2009-04-24 08:42:25
#include<iostream>
#include<cstring>
void main(){
using namespace std;
char str1,str2;
str1="hello";

strcpy(str2,"hello");
cout<<str2<<endl;
return ;
}
这是编译后的错误提示:
D:\vc6.0\vc6.0\MSDev98\MyProjects\ex_6_1\ex_6_1.cpp(8) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
...全文
96 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunchao365 2009-05-15
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstring>
void main()
{
using namespace std;
char *str1="hello";
char str2[10]; //用数组
strcpy(str2,str1);
cout < <str2 < <endl;
}

或者

#include <iostream>
#include <cstring>
using namespace std;
void main()
{
char *str1="hello";
char *str2=new char[10]; //动态申请内存
strcpy(str2,str1);
cout < <str2 < <endl;
free(str); //释放动态申请的内存
}

tangxu12 2009-04-27
  • 打赏
  • 举报
回复
char× str1,str2; //加一个×
koulongfei 2009-04-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 coolajie 的帖子:]
#include <iostream>
#include <cstring>
void main(){
using namespace std;
char str1,str2;
str1="hello";

strcpy(str2,"hello");
cout < <str2 < <endl;
return ;
}
这是编译后的错误提示:
D:\vc6.0\vc6.0\MSDev98\MyProjects\ex_6_1\ex_6_1.cpp(8) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
[/Quote]
#include <iostream>
#include <cstring>
void main()
{
using namespace std;
char *str1="hello";
char str2[10]; //用数组
strcpy(str2,str1);
cout<<str2<<endl;
}

或者

#include <iostream>
#include <cstring>
using namespace std;
void main()
{
using namespace std;
char *str1="hello";
char *str2=new char[10]; //动态申请内存
strcpy(str2,str1);
cout<<str2<<endl;
}


koulongfei 2009-04-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 deltamaster 的回复:]
...
char *source = "Hello"; //此时source指向的目标是常量。
char *dest;
strcpy(dest, source); //参数dest指向的目标必须可以修改。
...
[/Quote]


参数dest指向的目标必须可以修改 对


这是编译连接都麽问题,可是没有运行结果的
koulongfei 2009-04-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xt1989 的回复:]
strcpy()函数的参数是地址,而不是字符串.
[/Quote]

xt1989 2009-04-25
  • 打赏
  • 举报
回复
strcpy()函数的参数是地址,而不是字符串.
deltamaster 2009-04-25
  • 打赏
  • 举报
回复

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
char *source = "Hello"; //此时source指向的目标是常量。
char *dest;
strcpy(dest, source); //参数dest指向的目标必须可以修改。
cout << dest << endl;
return 0;
}
wypstone 2009-04-25
  • 打赏
  • 举报
回复
- -.

书上的基础知识看的太少了吧···
hylove9494 2009-04-24
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

char str1,str2;
str1="hello";
str1和str2是字符,"hello"是字符串
CoolChina 2009-04-24
  • 打赏
  • 举报
回复
str2是char类型的不能用strcpy函数给他赋值,如果是char指针类型的就可以了
wwwypy 2009-04-24
  • 打赏
  • 举报
回复
楼上说的对!!或者改为string str1,str2就可以了。





jackyjkchen 2009-04-24
  • 打赏
  • 举报
回复
str2是字符,Hello是字符串,不一样的

char str2[6] = {0};这样声明就行了

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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