473,378 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

std::string to char*


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
6 2192
"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

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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
3
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char,...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
6
by: Khuong Dinh Pham | last post by:
How do i read the contents of a xml file and output it to a std::string. Thx in advance
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
6
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.