Topic: STL string and the string contents....


Author: "Peter Dimov" <pdimov@techno-link.com>
Date: 1999/04/18
Raw View
Ash <awbone@be.the.spam.mindspring.com> wrote in message
news:370f5f33.7312171@news.mindspring.com...
> >How can I get the contents, the string value, of a "string" variable into
a
> >"char *" or "const char *"?
> >
> >the following does not work in VC 6.
> >
> >#include <string>
> >
> >string s = "string value";
> >
> >char * t = (char *) s.c_str();
> >
> >error C2440: 'type cast' : cannot convert from 'const char *(__thiscall
> >std::basic_string<char,struct std::char_traits<char>,class
> >std::allocator<char> >::*' to 'char *'
> >        There is no context in which this conversion is possible
> >
> >
> >What am I doing wrong?

<snip>

> The C-style cast you use cannot cast away the const specifier.

I've always thought that it could.

Judging from the error above, the problem is that (char*) s.c_str() is
interpreted
by the compiler as ( (char*) s.c_str ) (), i.e. it tries to convert the
member function s.c_str to char*, which is obviously not allowed.

--
Peter Dimov
Multi Media Ltd.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Frederic LACHASSE <frederic.lachasse@dial.oleane.com>
Date: 1999/04/15
Raw View
"Reza Razavipour" <reza_razavipour@aspentech.com> wrote:
> How can I get the contents, the string value, of a "string" variable into a
> "char *" or "const char *"?
>
> the following does not work in VC 6.
>
> #include <string>
>
> string s = "string value";
>
> char * t = (char *) s.c_str();
>
> error C2440: 'type cast' : cannot convert from 'const char *(__thiscall
> std::basic_string<char,struct std::char_traits<char>,class
> std::allocator<char> >::*' to 'char *'
>         There is no context in which this conversion is possible
>
>
> What am I doing wrong?

std::string::c_str() returns a "const char*" and you cannot convert it to
"char*" without using const_cast<>, so you must do:

 char* t = const_cast<char*>(s.c_str());

However, what you probably want to do is:

 const char* t = s.c_str();

because modifying the content of the string through t has probably undefined
results. const_cast<> is to be used only when you know what you are doing,
not when you want to please the compiler.

 Frederic LACHASSE (ECP 86)
 Internet mailto:frederic.lachasse@dial.oleane.com
 CompuServe: 100530,2005
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: awbone@be.the.spam.mindspring.com (Ash)
Date: 1999/04/15
Raw View
>How can I get the contents, the string value, of a "string" variable into a
>"char *" or "const char *"?
>
>the following does not work in VC 6.
>
>#include <string>
>
>string s = "string value";
>
>char * t = (char *) s.c_str();
>
>error C2440: 'type cast' : cannot convert from 'const char *(__thiscall
>std::basic_string<char,struct std::char_traits<char>,class
>std::allocator<char> >::*' to 'char *'
>        There is no context in which this conversion is possible
>
>
>What am I doing wrong?

std::string::c_str() returns a const char* (see the first line of your
error message above).  The conversion rules prohibit you from
assigning a const char* to a char*.

depending on what you need to do,  you could either:

const char* t = s.c_str();

or

char* t = new char[s.size() + 1];

s.copy(t, s.size());
t[s.size()] = '\0';


The C-style cast you use cannot cast away the const specifier.

ashley
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: John_Maddock@compuserve.com (John Maddock)
Date: 1999/04/11
Raw View
>What am I doing wrong?

std::sting::c_str() returns a pointer to a const string, note that it
need not point to the actual memory used by std::sting - it could
point to a copy or whatever - so you should never attempt to modify
this sting.

Therefore make your pointer const:

const char* p = mystring.c_str();

John Maddock
http://ourworld.compuserve.com/homepages/John_Maddock/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: brahms@mindspring.com (Stan Brown)
Date: 1999/04/11
Raw View
[This followup was also e-mailed to the cited author.]

In this next-to-last year of the millennium,
reza_razavipour@aspentech.com (Reza Razavipour) wrote in comp.std.c++:
>#include <string>
>string s = "string value";
>char * t = (char *) s.c_str();
>
>error C2440: 'type cast' : cannot convert from 'const char *(__thiscall
>std::basic_string<char,struct std::char_traits<char>,class
>std::allocator<char> >::*' to 'char *'
>        There is no context in which this conversion is possible
>
>What am I doing wrong?

c_str() returns a const char*, that is, a mutable pointer to a non-
mutable array of characters. It's illegal to assign that to a char*, a
mutable pointer to an array of mutable characters, since that would
defeat the purpose of returning a const char* in the first place.

If you just want to look at the characters, change t to a const char*.

If you actually want to change them, you need make your own copy. Start
with
 char* t = new char[s.size()+1];
and then copy s.c_str() into that array.

--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
                                    http://www.mindspring.com/~brahms/
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/04/11
Raw View
Stan Brown wrote:
....
> c_str() returns a const char*, that is, a mutable pointer to a non-
> mutable array of characters. It's illegal to assign that to a char*, a
> mutable pointer to an array of mutable characters, since that would
> defeat the purpose of returning a const char* in the first place.

'mutable' is a C++ keyword with a very precise, definite meaning, and
that meaning is not a simple as "not const". I recommend choosing some
other phrase, such as 'modifiable', or 'changeable', or 'non-const', to
avoid confusion.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Reza Razavipour" <reza_razavipour@aspentech.com>
Date: 1999/04/10
Raw View
How can I get the contents, the string value, of a "string" variable into a
"char *" or "const char *"?

the following does not work in VC 6.

#include <string>

string s = "string value";

char * t = (char *) s.c_str();

error C2440: 'type cast' : cannot convert from 'const char *(__thiscall
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >::*' to 'char *'
        There is no context in which this conversion is possible


What am I doing wrong?

Reza
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]