Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const char* to char* conversion

Reply
Thread Tools

const char* to char* conversion

 
 
Perro Flaco
Guest
Posts: n/a
 
      06-19-2006
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!

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-19-2006
Perro Flaco wrote:
> 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.


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


 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      06-19-2006
Perro Flaco posted:


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



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).


--

Frederick Gotham
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-19-2006
Frederick Gotham wrote:
> Perro Flaco posted:
>
>
>> str2 = (char *)str1.c_str();

>
>
> 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).


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


 
Reply With Quote
 
Azumanga
Guest
Posts: n/a
 
      06-19-2006

Perro Flaco wrote:
> 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.
>

Depends what you want to do. In your current situation, you can do the
following:

1) You can pass str2 to functions that expect a char*. However it is
important that nothing changes the string at all.

2) You must not change str1 at all, until you have finished doing
things with str2. Any change to str1 could (and probably will) make the
pointer str2 invalid.

Chris

 
Reply With Quote
 
abose
Guest
Posts: n/a
 
      06-19-2006
Not sure about your intention, but if you want to play it safe adding a
const with str2 will help.

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

Victor Bazarov wrote:
> Frederick Gotham wrote:
> > Perro Flaco posted:
> >
> >
> >> str2 = (char *)str1.c_str();

> >
> >
> > 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).

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


 
Reply With Quote
 
Perro Flaco
Guest
Posts: n/a
 
      06-19-2006
Because there are other functions that need "char*" as input, so I get
errors when I do: str1.c_str()

I would like to convert a string to a char*, or a const char* to char*.
Any advice?

Thanks!


Victor Bazarov ha escrito:

> Perro Flaco wrote:
> > 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.

>
> 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


 
Reply With Quote
 
Perro Flaco
Guest
Posts: n/a
 
      06-19-2006
Ok, now I see what's going on

Thank you!


Azumanga ha escrito:

> Perro Flaco wrote:
> > 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.
> >

> Depends what you want to do. In your current situation, you can do the
> following:
>
> 1) You can pass str2 to functions that expect a char*. However it is
> important that nothing changes the string at all.
>
> 2) You must not change str1 at all, until you have finished doing
> things with str2. Any change to str1 could (and probably will) make the
> pointer str2 invalid.
>
> Chris


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      06-19-2006

Victor Bazarov wrote:
> Frederick Gotham wrote:
> > Perro Flaco posted:
> >
> >
> >> str2 = (char *)str1.c_str();

> >
> >
> > 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).

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


Nothing more nor less than what happens without the const_cast afaik.
The pointer you got from c_str() may or may not get hosed.

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      06-19-2006

Azumanga wrote:
> Perro Flaco wrote:
> > 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.
> >

> Depends what you want to do. In your current situation, you can do the
> following:
>
> 1) You can pass str2 to functions that expect a char*. However it is
> important that nothing changes the string at all.


And the fact that they accept char* instead of const char* indicates
that they will.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments