473,799 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string to char *

Hi,
how do I convert std::string to char * ?

regards,

-Ab.
Mar 6 '06 #1
5 1562
Hi,
how do I convert std::string to char * ?


you can use PtrToStringChar s
because all managed strings are in unicode, this will give you a unicode
string. you can then go from unicode to ansi if you like.

see MSDN for more details. it has an example that describes how to do this.

--

Kind regards,
Bruno.
br************* *********@hotma il.com
Remove only "_nos_pam"

Mar 6 '06 #2
> Hi,
how do I convert std::string to char * ?


Oops.
previous reply was for managed strings.
for std::string you can just use the c_str() method.

std::string myString("Hello ");

const char * ptr = myString.c_str( );

remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not
modified in any way.

--

Kind regards,
Bruno.
br************* *********@hotma il.com
Remove only "_nos_pam"

Mar 6 '06 #3
> remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not


"Bruno van Dooren" <br************ **********@hotm ail.com> wrote in message
news:CA******** *************** ***********@mic rosoft.com...
Hi,
how do I convert std::string to char * ?


Oops.
previous reply was for managed strings.
for std::string you can just use the c_str() method.

std::string myString("Hello ");

const char * ptr = myString.c_str( );

remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not
modified in any way.

--

Kind regards,
Bruno.
br************* *********@hotma il.com
Remove only "_nos_pam"

Mar 6 '06 #4
> const char * ptr = myString.c_str( );

And I wont have to worry about "delete"ing the ptr right?

-Ab.
"Bruno van Dooren" <br************ **********@hotm ail.com> wrote in message
news:CA******** *************** ***********@mic rosoft.com...
Hi,
how do I convert std::string to char * ?


Oops.
previous reply was for managed strings.
for std::string you can just use the c_str() method.

std::string myString("Hello ");

const char * ptr = myString.c_str( );

remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not
modified in any way.

--

Kind regards,
Bruno.
br************* *********@hotma il.com
Remove only "_nos_pam"

Mar 6 '06 #5
> > const char * ptr = myString.c_str( );

And I wont have to worry about "delete"ing the ptr right?


no. definitly not. the buffer that is returned is managed by the string
object itself.

That is also the reason why you shouldn't use it after the string changes.
The standard says that that pointer is only valid while the string stays the
same.

--

Kind regards,
Bruno.
br************* *********@hotma il.com
Remove only "_nos_pam"

Mar 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
22740
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 the std::string a pointer and a length and then give the std::string control over the pointer and its content? I'm basically trying to avoid copying large text between an std::string and a char pointer, and vice versa. Is there anyhing in the...
24
11039
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 <string> #include <vector> #include <cstring> // presume s from somewhere, such as:
3
2856
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, std::char_traits<char>, std::allocator<char> >' is an inaccessible base of `Path' I'm using Debians
16
16431
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
6645
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
4817
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
37
3819
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 is quite importante - it's not a realtime system, but almost - and I concern about std::string performance in terms of speed. No doubt to use std implementation is a lot easier but I can't sacrifice speed.
10
26649
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* and I want to give it a std::string no matching function for call to `MD5::update(std::string&, size_t)' candidates are: void MD5::update(unsigned char*, unsigned int) void PrintMD5(string str){
6
4862
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 szHostName;
13
3671
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 <string>
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.