Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6
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
     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 11-May-12 0:52am
prog786374
Edited 11-May-12 11:28am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
const int SIZE = 256;
char name[SIZE];
 
strncpy(name, testCStr, SIZE-1);
name[SIZE] = '\0';
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you're not going to modify the string contents, then you can simply cast the CString to a const pointer.
 
const char * name = (const char *) testCStr;
 
Or better,
 
LPCTSTR name = (LPCTSTR) testCStr;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can use sprintf() function
 
 
CString testCStr;
 
testCStr = "This is an example.";
 
char * chr = new char[testCStr.GetLength()];
 
sprintf(chr, "%s", testCStr);
 
  Permalink  
Comments
CPallini at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 520
1 Mathew Soji 335
2 BillWoodruff 260
3 OriginalGriff 215
4 Afzaal Ahmad Zeeshan 188
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 17 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100