Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All


I want to know that i get in a variable suppose CString testCStr in this variable i get the names of somes step by step

i want to pass this name in an array of character type
like
C++
  char name[];

name=testCStr;// at this point it gives me error

i want to name of CString variable into character type array and use it further operations.

how to transfer the strign in the variable of CString(testCStr). to the char aray variable name;

plz Help
Posted
Updated 11-May-12 10:28am
v2

If you really need that (in most cases you don't) then you have to copy the CString content.
(For simplicity I assume you have a ANSI CString, not a wide character one)
C++
const int SIZE = 256;
char name[SIZE];

strncpy(name, testCStr, SIZE-1);
name[SIZE] = '\0';
 
Share this answer
 
v2
If you're not going to modify the string contents, then you can simply cast the CString to a const pointer.

C++
const char * name = (const char *) testCStr;

Or better,

LPCTSTR name = (LPCTSTR) testCStr;
 
Share this answer
 
You can use sprintf() function

C++
CString testCStr;

testCStr = "This is an example.";

char * chr = new char[testCStr.GetLength()];

sprintf(chr, "%s", testCStr);
 
Share this answer
 
Comments
CPallini 17-May-12 3:42am    
Do you realize your code is flawed?
(Hint: GetLength does not include the string terminator).
Minor (maybe arguable) points:
- If you suggest using new then it would be nice remembering the OP he/her has to eventually call delete.
- sprintf is overkill for such a task.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900