const char* ch = (LPCTSTR)cstr;
ch指向的地址和cstr相同。但由於使用const保證ch不會修改,所以安全.
2.傳給未分配內存的指針.
char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
cstr.ReleaseBuffer();
//修改ch指向的值等於修改cstr裏面的值.
//PS:用完ch後,不用delete ch,因爲這樣會破壞cstr內部空間,容易造成程序崩潰.
3.把CString 值賦給已分配內存的char *。
int strLength = cstr1.GetLength() + 1;
char *pValue = new char[strLength];
strncpy(pValue, cstr1, strLength);
4.把CString 值賦給已分配內存char[]數組.
int strLength1 = cstr1.GetLength() + 1;
char chArray[100];
memset(chArray,0, sizeof(bool) * 100); //將數組的垃圾內容清空.
strncpy(chArray, cstr1, strLength1);