如题:代码如下:
char* str=(char*)malloc(100); //调试时分配区域的首地址为:str地址为XXXX0830
CString ch;
ch.Format("adsgdfgfd");
str=(LPSTR)(LPCTSTR)ch; //str地址变为:XXXXa894
------------------------------
为什么会变呢?将字符串存入数组不是这样做的吗?请大家帮忙想想!!!在线等待、、、、、、、、
char* str=(char*)malloc(100); //调试时分配区域的首地址为:str地址为XXXX0830
CString ch;
ch.Format("adsgdfgfd");
str=(LPSTR)(LPCTSTR)ch; //str地址变为:XXXXa894
------------------------------
为什么会变呢?将字符串存入数组不是这样做的吗?请大家帮忙想想!!!在线等待、、、、、、、、
CString ch;
ch.Format("adsgdfgfd");
strcpy(str, ch);
你的做法类似:
char *str1 = NULL;
char *str2 = xxx;
str1 = str2;
int nLength = WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL);
char *pBuffer = new char[nLength + 1];
WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),pBuffer,nLength,NULL,NULL);
pBuffer[nLength] = 0;方法2: CString s = _T("测试用例");
USES_CONVERSION;
char* lpsz = T2A(s.GetBuffer(s.GetLength()));
s.ReleaseBuffer(s.GetLength());
CString ch;
ch.Format("adsgdfgfd");
strcpy(str, (LPSTR)(LPCTSTR)ch);//str=(LPSTR)(LPCTSTR)ch; //str地址变为:XXXXa894
执行此句后,str = CString类型变量ch的地址值,当然和初始分配时的值不同了。
楼主可以单步跟踪验证。
sprintf(str,"adsgdfgfd");
这个方法可以赋值进去,可是在
free(str); //这个地方出现Debug Erroring
str=NULL;
随赋值进去,可是释放指针时Debug 错误