vc中如何将 ‘CString’ 转换成 ‘ char * ’ 类型
2015-10-10 10:43
232 查看
1.传给未分配内存的const char* (LPCTSTR)指针.
CString cstr(asdd);
const char* ch = (LPCTSTR)cstr;
ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全.
2.传给未分配内存的指针.
CString cstr = "ASDDSD";
char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
cstr.ReleaseBuffer();
//修改ch指向的值等于修改cstr里面的值.
//PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃.
3.第二种用法。把CString 值赋给已分配内存的char *。
CString cstr1 = "ASDDSD";
int strLength = cstr1.GetLength() + 1;
char *pValue = new char[strLength];
strncpy(pValue, cstr1, strLength);
4.第三种用法.把CString 值赋给已分配内存char[]数组.
CString cstr2 = "ASDDSD";
int strLength1 = cstr1.GetLength() + 1;
char chArray[100];
memset(chArray,0, sizeof(bool) * 100); //将数组的垃圾内容清空.
CString cstr(asdd);
const char* ch = (LPCTSTR)cstr;
ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全.
2.传给未分配内存的指针.
CString cstr = "ASDDSD";
char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
cstr.ReleaseBuffer();
//修改ch指向的值等于修改cstr里面的值.
//PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃.
3.第二种用法。把CString 值赋给已分配内存的char *。
CString cstr1 = "ASDDSD";
int strLength = cstr1.GetLength() + 1;
char *pValue = new char[strLength];
strncpy(pValue, cstr1, strLength);
4.第三种用法.把CString 值赋给已分配内存char[]数组.
CString cstr2 = "ASDDSD";
int strLength1 = cstr1.GetLength() + 1;
char chArray[100];
memset(chArray,0, sizeof(bool) * 100); //将数组的垃圾内容清空.
相关文章推荐
- c#入门之类型转换详解
- C#难点逐个击破(9):类型转换
- VC实现获取当前正在运行的进程
- vc中SendMessage自定义消息函数用法实例
- VC实现让关闭按钮成灰色不可用的方法
- VC++中的字体设置方法详解
- VC程序设计中CreateProcess用法注意事项
- VC实现批量删除指定文件的方法
- C++中4种类型转换方式 cast操作详解
- vc中使用SendMessage自定义消息函数
- vc提示unexpected end of file found的原因分析
- vc获取计算机名和ip地址的方法
- C++中的四种类型转换
- VC6.0实现读取Excel数据的方法
- VC下通过系统快照实现进程管理的方法
- asp 类型转换函数大全第1/2页
- VC下实现fopen支持中文的方法
- JavaScript高级程序设计 阅读笔记(四) ECMAScript中的类型转换
- Javascript 强制类型转换函数
- 解析VC中预编译头文件的深入分析