| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 604 人关注过本帖
标题:【请教】 这两句话为什么不能换
只看楼主 加入收藏
piaohai09
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-7-16
收藏
 问题点数:0 回复次数:1 
【请教】 这两句话为什么不能换
#include<iostream>
#include<string>
int main()
{
using namespace std;
char animal[20]="bear";
const char* bird="wren";
char* ps;
cout<<animal<<" and ";
cout<<bird<<"\n";
cout<<"Enter a kind of animal:";
cin>>animal;
ps=animal;
cout<<ps<<"s!\n";
cout<<"before using strgy():\n";
cout<<animal<<"at"<<(int*)animal<<endl;
cout<<ps<<"at"<<(int*)ps<<endl;
ps=new char[strlen(animal)+1];//此处为什么不能用 ps=animal;代替
strcpy(ps,animal);
cout<<"after using strcpy():\n";
cout<<animal<<"at"<<(int *)animal<<endl;
cout<<ps<<"at"<<(int *)ps<<endl;
delete[]ps;
return 0;
}
搜索更多相关主题的帖子: include std animal using 
2007-07-17 14:06
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
ps=new char[strlen(animal)+1];//此处为什么不能用 ps=animal;代替
strcpy(ps,animal);

注意后面用到strcpy,这个函数要求两个char*做形参,并且大小要能兼容。
所以要用到上一句,给ps一个适合的大小。

Fight  to win  or  die...
2007-07-17 16:01
快速回复:【请教】 这两句话为什么不能换
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014303 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved