| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 554 人关注过本帖
标题:string pointer的问题
只看楼主 加入收藏
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
收藏
 问题点数:0 回复次数:2 
string pointer的问题

想把一个string赋值到new string返回的地址空间

string s1="hello,world";
string s2=new string;
for(unsigned int i=0;i<=s1.length();i++) s2[i]=s1[i];

编译ok,但运行error,好像是指向了非法地址,这是什么道理?

搜索更多相关主题的帖子: pointer string 
2007-09-19 23:04
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(loriwang)string pointer的问题

You made more than 1 mistake here.
=======================================

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

int main()
{
string s1 = "hello,world";
string* s2 = new string;
s2->resize(s1.size());
for (unsigned int i = 0; i < s1.length(); i++)
{
s2->at(i) = s1[i];
}
cout << * s2 << endl;

return 0;
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-09-19 23:22
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
收藏
得分:0 

er.... resize(), at() function and C++ style string. I have checked some documents and I found that I confused char pointer and char array with string pointer and string. Thx HJin.


Nothing is everything
2007-09-20 00:31
快速回复:string pointer的问题
数据加载中...
 
   



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

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