const char *到char *转换 [英] const char* to char* conversion

查看:111
本文介绍了const char *到char *转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我有这个:


string str1;

char * str2 ;

....

str1 =" whatever";

....

str2 = (char *)str1.c_str();


这样可以吗?有没有其他更好的方法来做同样的事情?我不确定

如果我做的很重要,所以我希望你可以帮我这个。


先谢谢你!

Hi!

I''ve got this:

string str1;
char * str2;
....
str1 = "whatever";
....
str2 = (char *)str1.c_str();

Is this ok? Is there any other better way to do the same? I''m not sure
if I''m doing the rigth thing, so I hope you can help me with this.

Thanks in advanced!

推荐答案

Perro Flaco写道:
Perro Flaco wrote:
我有这个:

string str1;
char * str2;
......
str1 =" whatever";
...
str2 =(char *)str1。 c_str();

这可以吗?有没有其他更好的方法来做同样的事情?我不确定
我是否正在做这件事,所以我希望你能帮我这个。
I''ve got this:

string str1;
char * str2;
...
str1 = "whatever";
...
str2 = (char *)str1.c_str();

Is this ok? Is there any other better way to do the same? I''m not sure
if I''m doing the rigth thing, so I hope you can help me with this.




不,它'不行。而且,不,没有更好的这样做的方式。无论你如何尝试,它都很糟糕

。为什么你认为你需要那个?


V

-

请在回复时删除资金''A'通过电子邮件

我没有回复最热门的回复,请不要问



No, it''s not OK. And, no, there is no "better" way to do it. It''s bad
no matter how you try doing it. Why do you think you need that?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Perro Flaco发布:

Perro Flaco posted:

str2 =(char *)str1.c_str();
str2 = (char *)str1.c_str();



str2 = const_cast< char *>(str1.c_str( ));

我认为只要你不使用str2,它就是有效的。改变数据

(但我可以更正)。

-


Frederick Gotham


Frederick Gotham写道:
Frederick Gotham wrote:
Perro Flaco发布:

Perro Flaco posted:

str2 =(char *) str1.c_str();
str2 = (char *)str1.c_str();



str2 = const_cast< char *>(str1.c_str());

我认为它是有效的只要你不使用str2改变
数据(但我可以更正)。


str2 = const_cast<char*>( str1.c_str() );
I think it''s valid just so long as you don''t use "str2" to alter the
data (but I''m open to correction).




如果使用''str1''来改变数据怎么办? br />



What if you use ''str1'' to alter the data?


这篇关于const char *到char *转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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