By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

std::string to char*

P: n/a

Well, here is my problem, I'm trying to convert an std::string variable to
a char*, when I do
std::string foo = "try something";
char* foo2 = foo.c_str();

an error ocurre, c_str() return a const char* not a char* how can I
resolve this problem, what's the diference between const char* and char* and
how can I obtatin a char*

Thanx in advantage
--
"The best way to predict the future, is to invent it"
Mar 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Hector Y. Martinez" <He*************@discussions.microsoft.com> wrote in
message news:E7**********************************@microsof t.com...
Well, here is my problem, I'm trying to convert an std::string variable
to
a char*, when I do
std::string foo = "try something";
char* foo2 = foo.c_str();

an error ocurre, c_str() return a const char* not a char* how can I
resolve this problem, what's the diference between const char* and char*
and
how can I obtatin a char*


I'm not sure what you are asking exactly.

To fix you error you can simply change the declaration

const char *foo2 = foo.c_str();

To understand if that's appropriate in your case, you might want to look up
"const correctness" in your favorite C++ text or on the web. FWIW: this
article came up first in my search just now

http://www.possibility.com/Cpp/const.html

If on the other hand, you really want a char *, what you can do is to
allocate a string of the proper length and then copy the characters:

char *q = new char[ str.length() + 1];
strcpy(q, str.c_str() );

Regards,
Will

Mar 16 '06 #2

P: n/a

Thanx, that's was exactly what I was asking for... now I have another
question, how can I convert an char* , const char* , or std::string to int ??
Thnx in advantage
--
"The best way to predict the future, is to invent it"
"William DePalo [MVP VC++]" wrote:
"Hector Y. Martinez" <He*************@discussions.microsoft.com> wrote in
message news:E7**********************************@microsof t.com...
Well, here is my problem, I'm trying to convert an std::string variable
to
a char*, when I do
std::string foo = "try something";
char* foo2 = foo.c_str();

an error ocurre, c_str() return a const char* not a char* how can I
resolve this problem, what's the diference between const char* and char*
and
how can I obtatin a char*


I'm not sure what you are asking exactly.

To fix you error you can simply change the declaration

const char *foo2 = foo.c_str();

To understand if that's appropriate in your case, you might want to look up
"const correctness" in your favorite C++ text or on the web. FWIW: this
article came up first in my search just now

http://www.possibility.com/Cpp/const.html

If on the other hand, you really want a char *, what you can do is to
allocate a string of the proper length and then copy the characters:

char *q = new char[ str.length() + 1];
strcpy(q, str.c_str() );

Regards,
Will


Mar 16 '06 #3

P: n/a
Hector Y. Martinez wrote:
Thanx, that's was exactly what I was asking for... now I have another
question, how can I convert an char* , const char* , or std::string
to int ??


Let's rephrase that question: How can I convert a sequence of digits into
an integer?

You have a number of choices:

atoi - standard C library function
sscanf - standard C library function
std::stringstream - standard C++ library class
boost::lexical_cast - boost library

The easiest for simple purposes is atoi, while std:stringstream and
boost::lexical_cast offer much more flexibility.

-cd
Mar 16 '06 #4

P: n/a
Hi William,
const char *foo2 = foo.c_str();
Is there a reason why not do const_cast?
const char *foo2 = const_cast<char*>(foo.c_str());
"William DePalo [MVP VC++]" <wi***********@mvps.org> schrieb im Newsbeitrag
news:Om**************@TK2MSFTNGP14.phx.gbl...
"Hector Y. Martinez" <He*************@discussions.microsoft.com> wrote in
message news:E7**********************************@microsof t.com...
Well, here is my problem, I'm trying to convert an std::string variable
to
a char*, when I do
std::string foo = "try something";
char* foo2 = foo.c_str();

an error ocurre, c_str() return a const char* not a char* how can I
resolve this problem, what's the diference between const char* and char*
and
how can I obtatin a char*


I'm not sure what you are asking exactly.

To fix you error you can simply change the declaration

const char *foo2 = foo.c_str();

To understand if that's appropriate in your case, you might want to look
up "const correctness" in your favorite C++ text or on the web. FWIW:
this article came up first in my search just now

http://www.possibility.com/Cpp/const.html

If on the other hand, you really want a char *, what you can do is to
allocate a string of the proper length and then copy the characters:

char *q = new char[ str.length() + 1];
strcpy(q, str.c_str() );

Regards,
Will


Mar 16 '06 #5

P: n/a
"Boni" <no****@parampam.pam> wrote in message
news:eO**************@TK2MSFTNGP11.phx.gbl...
const char *foo2 = foo.c_str();
Is there a reason why not do const_cast?
const char *foo2 = const_cast<char*>(foo.c_str());


Um, because it is an ugly hack? :-)

Seriously, if you could write

std::string str="hello";

char *q = const_cast<char *>( str.c_str() );

*q = 'B';

where would that leave str? Suppose you used that non-const pointer as a
target for a string copy operation where the source length exceeds the
allocation of the string? Nothing good can come of that kind of hackery.

Regards,
Will


Mar 16 '06 #6

P: n/a
Carl Daniel [VC++ MVP] wrote:
Hector Y. Martinez wrote:
Thanx, that's was exactly what I was asking for... now I have another
question, how can I convert an char* , const char* , or std::string
to int ??

Let's rephrase that question: How can I convert a sequence of digits into
an integer?

You have a number of choices:

atoi - standard C library function


I'll add
strtol - standard C library function with improved error reporting.
sscanf - standard C library function
std::stringstream - standard C++ library class
boost::lexical_cast - boost library

The easiest for simple purposes is atoi, while std:stringstream and
boost::lexical_cast offer much more flexibility.


I don't like atoi, since it signals an error by returning the valid
value 0, and it can't handle underflow or overflow.

Tom
Mar 17 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.