std::string to char*

G

Guest

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
 
W

William DePalo [MVP VC++]

Hector Y. Martinez said:
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
 
G

Guest

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 said:
Hector Y. Martinez said:
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
 
C

Carl Daniel [VC++ MVP]

Hector said:
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
 
B

Boni

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 said:
Hector Y. Martinez said:
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
 
W

William DePalo [MVP VC++]

Boni said:
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
 
T

Tom Widmer [VC++ MVP]

Carl said:
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
 

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

Top