const char* to char* conversion

P

Perro Flaco

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

Victor Bazarov

Perro said:
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
 
F

Frederick Gotham

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

Victor Bazarov

Frederick said:
Perro Flaco posted:




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

Azumanga

Perro said:
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
 
A

abose

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();
 
P

Perro Flaco

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

Noah Roberts

Victor said:
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.
 
N

Noah Roberts

Azumanga said:
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.
 
S

Sgt. York

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

Exactly. I see nothing but disaster in store for wanting to cast away
this particular const.
 
D

Daniel T.

"Perro Flaco said:
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!

A better way to do it:

string str1;
vector<char> str2;

str1 = "whatever";

copy( str1.begin(), str1.end(), back_inserter( str2 ) );
 
R

red floyd

Daniel said:
A better way to do it:

string str1;
vector<char> str2;

str1 = "whatever";

copy( str1.begin(), str1.end(), back_inserter( str2 ) );

You forgot:

str2.push_back('\0');
APICall(&str2[0]);
 
A

Andrey Tarasevich

Perro said:
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?


When some function need a 'char*' (as opposed to 'const char*') as an input,
this usually means one of two things:

1) The function needs to modify the data pointed by that 'char*' pointer. In
this can what you are trying to do is simply useless, because strings of
'std::string' type cannot be modified through the pointer returned by 'c_str()'.

Functions that modify the data pointed by 'char*' parameter are not compatible
with 'std::string' at all. The only safe way around is to convert the
'std::string' to a standalone modifiable zero-terminated C-style string, pass it
to the function and then convert the results back to 'std::string'.

2) The function parameter is declared improperly. In other words, the function
doesn't really need to modify the data, i.e. it doesn't really need a 'char*'
and a 'const char*' would work just as well. The better way to resolve the
problem in this case would be to change the function's parameter declaration
from 'char*' to the more appropriate 'const char*', assuming this is possible.
Otherwise, the 'const_cast' of 'c_str()' result to 'char*' type (mentioned by
others) would work, but it is rather ugly.
 
V

Victor Bazarov

Daniel said:
A better way to do it:

string str1;
vector<char> str2;

str1 = "whatever";

copy( str1.begin(), str1.end(), back_inserter( str2 ) );

If str2 is not used up until here, the form

string str1;
..
str1 = "whatever";
..
vector<char> str2(str1.begin(), str1.end());

is actually better, IMO. It can't be helped, of course, if 'str2' is
a member of the same class as 'str1'.

We have, however, already seen the reason why the OP needed the pointer
and vector is not a good substitute.

V
 
P

Perro Flaco

Yes, this is what I've done. We don't need the "char*" and a "const
char*" would do a perfect work.

Thanks!


Andrey Tarasevich ha escrito:
 
J

Jim Langston

Sgt. York said:
Exactly. I see nothing but disaster in store for wanting to cast away
this particular const.

I agree. Unfortunately I use a library with a header which is not const
correct. I've been trying to get the library developer to fix his const
correctness, but so far to little luck.

I wind up using things like:

somefunction( blah, blah, const_cast<char*>( MyString.c_str() ), blah );
and it's quite ugly and I don't like it, but have little choice until he
fixes his library (or I hack the header).

Which brings up a question that is probably OT for this newsgroup. If a
program is linking to a .lib and using a header, can the header be changed
to const correctness even if the calls in the .dll aren't const correctness?
That is, if it is known for a fact that the .dll won't change the data of a
char*, can changing the header to a const char * cause problems? If this is
OT (which it is) just ignore it please.
 
V

Victor Bazarov

Jim said:
[..]
If a program is linking to a .lib and using a header, can the header
be changed to const correctness even if the calls in the .dll aren't
const correctness?

Only if the linkage of those functions is "C". Otherwise, the type of
the arguments is part of the type of the function, and likely its name.
That is, if it is known for a fact that the .dll
won't change the data of a char*, can changing the header to a const
char * cause problems? If this is OT (which it is) just ignore it
please.

Yes, it can cause problems. And, yes, it is slightly OT because it's
implementation- and platform-specific.

V
 
S

Sgt. York

Jim said:
I agree. Unfortunately I use a library with a header which is not const
correct. I've been trying to get the library developer to fix his const
correctness, but so far to little luck.

I wind up using things like:

somefunction( blah, blah, const_cast<char*>( MyString.c_str() ), blah );
and it's quite ugly and I don't like it, but have little choice until he
fixes his library (or I hack the header).

I would go out of my way to avoid this. I would use a temporary despite
the performance hit.

-York
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top