for(i=4;i<=intSwitchCount+4;i++){
CString strSwitchNO,str;
if( i == intSwitchCount+4 ){
lpszHeaders[i] = NULL;
}
else{
str.Format("%d",i-3);
strSwitchNO = "开关" + str;
TCHAR* tch = (LPTSTR)(LPCTSTR)strSwitchNO;
lpszHeaders[i] = tch;
}
}
上面这段代码在运行时lpszHeaders[i]可以得到正确的值,但是当出了else语句之后lpszHeaders[i]中的值就没有了。请帮忙看看是什么原因?谢谢!
CString strSwitchNO,str;
if( i == intSwitchCount+4 ){
lpszHeaders[i] = NULL;
}
else{
str.Format("%d",i-3);
strSwitchNO = "开关" + str;
TCHAR* tch = (LPTSTR)(LPCTSTR)strSwitchNO;
lpszHeaders[i] = tch;
}
}
上面这段代码在运行时lpszHeaders[i]可以得到正确的值,但是当出了else语句之后lpszHeaders[i]中的值就没有了。请帮忙看看是什么原因?谢谢!
lpszHeaders[i] = NULL;
你这句话是干嘛用的?
srtSwitchNO是局部变量,for循环结束后就消失了。
所以lpszHeaders[i]给没有东西可指了。
把{号写在语句最后看起来真不习惯
头昏眼花
受不了
偶还是喜欢for(.......)
{
.......
.......
}
看起来舒服呵呵
lpszHeaders[i]指向CString 类型的一个局部变量所指向的字符串,这个局部变量只是在for语句的作用域有效,程序离开for的作用域后CString释放,lpszHeaders[i] 指向的内存也被释放了,所以出现了你说的情况改:
for(i=4;i<=intSwitchCount+4;i++){
CString strSwitchNO,str;
if( i == intSwitchCount+4 ){
lpszHeaders[i] = NULL;
}
else{
str.Format("%d",i-3);
strSwitchNO = "开关" + str;
TCHAR * tch = new TCHAR[ str.GetLength() + 1);
strcpy(tch , str);
lpszHeaders[i] = tch;
}
}// 使用完要记着释放lpszHeaders[i]