Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

const char * to char *

 
 
Steven C
Guest
Posts: n/a
 
      10-09-2003
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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      10-09-2003

"Steven C" <> wrote in message news: om...

> 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<char> tmp(strSome.begin(), strSome.end());
the3rdpartlib = &tmp[0];
>
> 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.


 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      10-09-2003
"Steven C" <> wrote in message
news: om...
> 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
--
jb

(replace y with x if you want to reply by e-mail)


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-09-2003


Steven C wrote:
>
> 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;


--
Karl Heinz Buchegger

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      10-09-2003
(Steven C) wrote in
news: om:

> 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.
 
Reply With Quote
 
klaas
Guest
Posts: n/a
 
      10-09-2003
Steven C wrote:

> 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

 
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
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
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
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Is char** (or char*[]) implicitly convertible to 'const char * const *'? kevin.hall@motioneng.com C Programming 24 10-30-2005 08:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57