CString to char Array?

How do I copy a CString to a Char array?

CString m_elsetSlotString;

char *msg;

m_elsetSlotString = ((LPCSTR)"SLOT 0");

msg[0] = m_elsetSlotString; // Doesn't work
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

AxterConnect With a Mentor Commented:
>>How do I copy the upper portion of the strhello string
>>using strncpy.
The pointer to strhello can be treated like a normal "const char*" pointer.

CString strHello = "Hello";
char test[32];
strcpy(test, (LPCTSTR)strHello);//Normal copy
AfxMessageBox(test);//Show contents of test

//copy starting at the third letter in the string
strcpy(test,((LPCTSTR)strHello) + 2 );//Test will equal "llo"
AfxMessageBox(test);//Show contents of test
msg = LPCSTR(m_elsetSlotString)
Easier (and clearer):

CString strHello = "Hello";
char test[32]; // or use new and sizeof for dynamic memory allocation
strncpy(test, strHello, sizeof(strHello));
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

You should use a cast in the strcpy command and the GetLength() function to determine the size.
CString strHello = "Hello";
char test[32];
strncpy(test, (LPCTSTR)strHello, strHello.GetLength());

strcpy(test, strHello.operator LPCTSTR(), strHello.GetLength());

CString m_elsetSlotString;
//The following method is used to get a constant pointer to a CString object
const char *msg = m_elsetSlotString.operator LPCTSTR();

//The following method is used to get a pointer to a CString object
//You should use this method if you want to modify the data that msg is pointing to
char *msg = m_elsetSlotString.LockBuffer();
//Do something with msg variable here.

//Now that you're done , you need to UnLock the buffer

You can also use GetBuffer() and ReleaseBuffer() functions for similar above requirements.
Just to clarify Axter's comment, if the string you want can be constant, then define it as const char* and the conversion is implicit (that is, the operator LPCTSTR() is called without you needing to specify if)

msg = m_elseSlotString;
hi zhjackal,
Welcome to Experts Exchange.  Please check the guidelines on answering questions.  See: http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#6

The experts in this topic area have agreed to make their posts as Comments, rather than Answers.   So please post Comments, like everybody else.  Thanks!

Anyway, your "answer" simply sets up a pointer to the char array (in an unsafe sort of way), it does not copy its data.

-- Dan
GoldStrikeAuthor Commented:
Axter thanks that worked,

I will award you the points but I had a follow on question which is why I increased the value.

How do I copy the upper portion of the strhello string using strncpy.

In other words, let's say strhello contains "hello", but I only wanted to copy "llo" to the test array, How would I accomplish this.

thanks for all the inputs,
I'm sorry if I screwed this up, maybe this should have been a seperate question.
Why copy?
If you use


it will drop a "char *" in your lap.
GoldStrikeAuthor Commented:
Thanks Axter
All Courses

From novice to tech pro — start learning today.