标题: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-171361-1-1.html




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

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