const char * to char *

S

Steven C

I have a 3rd party libary that takes a char * (not a const char *).

if I have:

string strSome;
char *the3rdpartylib;

how do I:
the3rdpartylib = strSome.c_str();

I have done:
the3rdpartylib = (char *) strSome.c_str();

which gets by the compiler messages but the lint program is complaining about
never assign a const to a non const.

I tried this:
the3rdpartylib = static_cast <char *> (strSome.c_str());

which gives compiler error message:
can't convert from const char * to char *

thanks
 
R

Ron Natalie

Steven C said:
I have done:
the3rdpartylib = (char *) strSome.c_str();

which gets by the compiler messages but the lint program is complaining about
never assign a const to a non const.

And lint is right. If your third party lib changes the string, you have undefined behavior.
Your best bet is to temporarily copy the string to some non-const place:

vector said:
I tried this:
the3rdpartylib = static_cast <char *> (strSome.c_str());

which gives compiler error message:
can't convert from const char * to char *

static_cast can't remove const.

const_cast<char*>(strSome.c_str())

is the same as the C-style cast you did above.
 
J

Jakob Bieling

Steven C said:
I have a 3rd party libary that takes a char * (not a const char *).

if I have:

string strSome;
char *the3rdpartylib;

how do I:
the3rdpartylib = strSome.c_str();

I have done:
the3rdpartylib = (char *) strSome.c_str();

which gets by the compiler messages but the lint program is complaining about
never assign a const to a non const.

I tried this:
the3rdpartylib = static_cast <char *> (strSome.c_str());

which gives compiler error message:
can't convert from const char * to char *


You need to apply a const_cast, because you want to cast away
const-ness. But you should only do this if you know that the 3rd party
library does not modify the string you are passing. If it does, your code
might not behave the way you would expect it to.

hth
 
K

Karl Heinz Buchegger

Steven said:
I have a 3rd party libary that takes a char * (not a const char *).

if I have:

string strSome;
char *the3rdpartylib;

how do I:
the3rdpartylib = strSome.c_str();

I have done:
the3rdpartylib = (char *) strSome.c_str();

which gets by the compiler messages but the lint program is complaining about
never assign a const to a non const.

I tried this:
the3rdpartylib = static_cast <char *> (strSome.c_str());

which gives compiler error message:
can't convert from const char * to char *

You want either of 2 things:
1) cast away the const ness
2) create a duplicate of the string as a C-style string

Which strategy you choose, depends largely on what the third
party library does with that C-style string:
* If it leaves it alone and just does read accesses to that string
you can take approach 1)
* If the library modifies the passed string, you have to resort
to approach 2)

for 1) the3rdpartylib = const_cast< char* > ( strSome.c_str() );

for 2) size_t len = strSome.length();
char* tmp = new char [ len + 1 ];
strcpy( tmp, strSome.c_str() );

... call the function

strSome = tmp;
delete [] tmp;
 
A

Andre Kostur

(e-mail address removed) (Steven C) wrote in
I have a 3rd party libary that takes a char * (not a const char *).

if I have:

string strSome;
char *the3rdpartylib;

how do I:
the3rdpartylib = strSome.c_str();

I have done:
the3rdpartylib = (char *) strSome.c_str();

which gets by the compiler messages but the lint program is
complaining about never assign a const to a non const.

I tried this:
the3rdpartylib = static_cast <char *> (strSome.c_str());

which gives compiler error message:
can't convert from const char * to char *

You're looking for:

the3rdpartylib = const_cast<char *>(strSome.c_str());

There's a couple of things to keep in mind:
1) Ensure that the 3rd party library doesn't try to write into the string
2) Ensure that you don't call any non-const methods on the string between
the time you use c_str(), and whenever the pointer obtained by c_str()
may be used.
 
K

klaas

Steven said:
I have a 3rd party libary that takes a char * (not a const char *).

if I have:

string strSome;
char *the3rdpartylib;

how do I:
the3rdpartylib = strSome.c_str();

I have done:
the3rdpartylib = (char *) strSome.c_str();

which gets by the compiler messages but the lint program is complaining about
never assign a const to a non const.

I tried this:
the3rdpartylib = static_cast <char *> (strSome.c_str());

which gives compiler error message:
can't convert from const char * to char *

thanks

what compiler do you use?
If you are using either borland c++ or microsoft visual c++ you are
without guarantees that you get proper behaviour on this... I'm afraid
g++ should understand such things...

good luck

ebone
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top