使用char *创建动态增长的字符串 [英] creating dynamically growing string using char*

查看:157
本文介绍了使用char *创建动态增长的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在创建一个指向char的指针:

char * lDirName = new char;

我是试图逐个字符地设置值。

lDirName [0] =''c'';

lDirName [1] ='':'';

lDirName [2] =''\\'';


问题在于,lDirName的内存地址是指向

中有一些垃圾值。所以代替lDirName而不是lDirName。取值为

" c:\"它取值为c:\1或者记忆中的任何东西。


我不能使用静态数组,因为我不知道多长时间lDirName

将会是。


如何克服这个问题?


最好的问候,

Shal

Hi,

I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= ''c'';
lDirName[1]= '':'';
lDirName[2]= ''\\'';

The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.

I can''t use static array because I don''t know how long "lDirName "
going to be.

how to overcome this problem?

Best Regards,
Shal

推荐答案

sh ***** ***@gmail.com 写道:




我正在创建一个指向char的指针:

char * lDirName = new char;

我试图逐个字符地设置值。

lDirName [0] =''c '';

lDirName [1] ='':'';

lDirName [2] =''\\'';


问题在于,lDirName的内存地址是指向

中有一些垃圾值。所以代替lDirName而不是lDirName。取值为

" c:\"它取值为c:\1或者记忆中的任何东西。


我不能使用静态数组,因为我不知道多长时间lDirName

将会是。


如何克服这个问题?
Hi,

I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= ''c'';
lDirName[1]= '':'';
lDirName[2]= ''\\'';

The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.

I can''t use static array because I don''t know how long "lDirName "
going to be.

how to overcome this problem?



使用std :: string。

use std::string.


谢谢,但我不想使用std :: string。如何才能让事情变得更好

而不使用它?


red floyd写道:
thanks, but i don''t want to use std::string. how can make things better
without using it?

red floyd wrote:
sh ******** @ gmail.com 写道:




我正在创建一个指向char的指针:

char * lDirName = new char;

我正在尝试逐字符设置值。

lDirName [0] =''c'';

lDirName [1] ='':'';

lDirName [2] =''\\'';


问题在于,lDirName的内存地址是指向

中有一些垃圾值。所以代替lDirName而不是lDirName。取值为

" c:\"它取值为c:\1或者记忆中的任何东西。


我不能使用静态数组,因为我不知道多长时间lDirName

将会是。


如何克服这个问题?
Hi,

I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= ''c'';
lDirName[1]= '':'';
lDirName[2]= ''\\'';

The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.

I can''t use static array because I don''t know how long "lDirName "
going to be.

how to overcome this problem?



使用std :: string。


use std::string.




< sh ******** @ gmail.comwrote in message

news:11 ********************* @ q16g2000cwq.googlegro ups.com ...

<sh********@gmail.comwrote in message
news:11*********************@q16g2000cwq.googlegro ups.com...




我正在创建一个指向char的指针:

char * lDirName = new char;

我正在尝试逐字符设置值。

lDirName [0] =''c'';

lDirName [1] ='':'';

lDirName [2] =''\\'';


问题在于,lDirName的内存地址是指向

中有一些垃圾值。所以代替lDirName而不是lDirName。取值为

" c:\"它取值为c:\1或者记忆中的任何东西。


我不能使用静态数组,因为我不知道多长时间lDirName

将会是。


如何克服这个问题?
Hi,

I am creating a pointer to char:
char * lDirName = new char;
I am trying to set values character by character.
lDirName[0]= ''c'';
lDirName[1]= '':'';
lDirName[2]= ''\\'';

The problem is that, the memory address that "lDirName " is pointing to
has some junk value in it. so instead of "lDirName" taking value as
"c:\" it takes value as "c:\1" or anything that is there in the memory.

I can''t use static array because I don''t know how long "lDirName "
going to be.

how to overcome this problem?



如果你不能使用std :: string出于某种原因,你可以试试这个:

If you can''t use std::string for some reason, then you could try this:


获取原始字符串的长度

new []一个char数组'两个较大的(一个用于新角色,

一个用于NULL终结符)

将旧复制到新的

追加新字符和NULL终结符

delete []旧字符串

设置原始指针指向新字符串
get the length of the original string
new[] an array of char that''s two larger (one for the new character, and
one for the NULL-terminator)
copy the old to the new
append the new character and a NULL-terminator
delete[] the old string
set the original pointer to point to the new string



(您还应该使用[1]新建原始字符串,并使用

""初始化它,以便它已经有一个NULL终止符。)


或者,假设这是某种循环,你可以使用两个循环,一个

,其唯一的工作是确定你需要的字符串的长度,

然后另一个实际填充它(在为
分配足够的空间之后)
字符串,加上一个用于NULL终止符。)


(可能还有其他方法可以实现你想要的,但你还没有

向我们展示了你如何获得这些个性化角色,所以很难猜到该领域的任何改进。


在我看来,现在应该很明显,从上面的工作开始,如果没有其他的话,使用std :: string是更好的选择。


-Howard

(You should also new your original string with [1], and initialize it with
"", so that it has a NULL-terminator already.)

Or, assuming that this is in some kind of loop, you could use two loops, one
whose sole job is to determine the length of the string that you''ll need,
and then another to actually fill it (after allocating enough space for the
string, plus one for the NULL-terminator).

(There may also be other ways to accomplish what you want, but you haven''t
shown us how you''re getting those individual characters, so it would be
difficult to guess on any improvements in that area.)

In my opinion, it should now be obvious, from the work required above if
nothing else, that using std::string is the better choice.

-Howard


这篇关于使用char *创建动态增长的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆