Another way of doing it would be:
CString data = "somestring";
char * data_char = data.LockBuffer();
//Code using data_char
data.UnlockBuffer(); //After data_char 's work is done.
Main Topics
Browse All TopicsHi,
I use this method to convert CString to char*
CString data;
const char* data_char = (LPCTSTR) data;
but this method works only if i use const char*. This means i cant manipulate it rite?
how do i make it into char * instead of const char* ?
or, can someone suggest other method?
thx
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
remember that the pointer returned by LPCTSTR operator is valid during life time of CString object.
copy the content of CString to your char pointer if you're to use it after the CString object life time.
e.g.
const char* data;
{
CString csTemp("vijay");
data = (LPTSTR)(LPCTSTR)csTemp;
//data has string value
....
....
.....
}//CString object life time ends here
// from here data is pointing to string anymore
some points to remeber before using Buffer functions of CString.
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions
The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated.
The buffer memory will be freed automatically when the CString object is destroyed.
Business Accounts
Answer for Membership
by: lemmeCPosted on 2004-07-18 at 04:06:14ID: 11577514
CString data="somestring";
char * data_char= new char[data.GetLength()+1];
strcpy(data_char, data);