C++中char类型数组的长度问题
2017-11-23 15:57
357 查看
char* a =new char[30]; cout<<"未初始化char数组--sizeof = "<<sizeof(a)<< endl; cout<<"未初始化char数组--strlen = "<<strlen(a)<< endl; cout<<"**************************"<<endl; char* b = "hello"; cout<<"hello 数组--sizeof = "<<sizeof(b)<< endl; cout<<"hello 数组--strlen = "<<strlen(b)<< endl; cout<<"**************************"<<endl; char* c; cout<<"char数组指针--sizeof = "<<sizeof(c)<< endl; //cout<<"char数组指针--strlen = "<<strlen(c)<< endl; 编译通过,运行出错 cout<<"**************************"<<endl; char* d =new char[30]; d = "hello"; cout<<"为已开辟空间的数组赋值--sizeof = "<<sizeof(d)<< endl; cout<<"为已开辟空间的数组赋值--strlen = "<<strlen(d)<< endl; cout<<"**************************"<<endl; char* e =new char[30]; e[0] = 'h'; e[1] = 'e';e[2] = 'l';e[3] = 'l';e[4] = 'o';e[5] = '\0'; cout<<"hello 数组 分配的内存未填满,且加\0--sizeof = "<<sizeof(e)<< endl; cout<<"hello 数组 分配的内存未填满,且加\0--strlen = "<<strlen(e)<< endl; cout<<"**************************"<<endl; char* ee =new char[30]; ee[0] = 'h'; ee[1] = 'e';ee[2] = 'l';ee[3] = 'l';ee[4] = 'o';ee[5] = '\0'; ee[6] = '\0'; cout<<"两个\\0的char数组--sizeof = "<<sizeof(ee)<< endl; cout<<"两个\\0的char数组--strlen = "<<strlen(ee)<<endl; cout<<"**************************"<<endl; char* eee =new char[30]; eee[0] = 'h'; eee[1] = 'e';eee[2] = 'l';eee[3] = 'l';eee[4] = 'o'; cout<<"未手动添加\\0的char数组--sizeof = "<<sizeof(eee)<< endl; cout<<"未手动添加\\0的char数组--strlen = "<<strlen(eee)<<endl; cout<<"**************************"<<endl; char* eeee =new char[30]; for(int i =0;i<30;i++) { eeee[i] ='h'; } cout<<"数组分配内存填满,未加\\0--sizeof = "<<sizeof(eeee)<< endl; cout<<"数组分配内存填满,未加\\0--strlen = "<<strlen(eeee)<< endl; cout<<"**************************"<<endl; char* eeeee =new char[30]; for(int i =0;i<29;i++) { eeeee[i] ='h'; } cout<<"数组分配内存填满,加\\0--sizeof = "<<sizeof(eeeee)<< endl; cout<<"数组分配内存填满,加\\0--strlen = "<<strlen(eeeee)<< endl; cout<<"**************************"<<endl; string s("hello"); cout<<"hello string--sizeof = "<<sizeof(s)<< endl; cout<<"hello string--strlen = "<<strlen(s.c_str())<< endl;
相关文章推荐
- char*数组(c类型字符串)和c++中的string的转换、字符串比较、char*数组等问题
- char*数组(c类型字符串)和c++中的string的转换、字符串比较、char*数组等问题
- 将C++的std::string类型变量赋值给char类型数组
- c语言 char*类型作为中间变量将许多字符串保存到一个数组的问题
- C++中,类Class数组长度的问题
- char 类型数组的初始化问题
- c/c++和java中关于char的长度问题
- c++中数组类型和长度的简单认识
- 【C++菜鸟问题】32/64为数据类型长度+sizeof
- C++中char类型的溢出问题
- 【语言基础】c++ 基本数据类型与字节数组(string,char [] )之间的转化方法
- 求C++数组长度问题
- C++ 中TCHAR字符串数组转化为Char类型数组
- char*数组(c类型字符串)和Vc++中的CString的转换,char*数组等问题
- C++ 二级指针、函数指针与数组复合类型的问题分析
- VC++编程中判断char类型数组的长度
- C++中 关于结构体中有char *类型的指针 赋值问题
- C++数据类型长度问题
- C/C++关于字符串string、char*字符串数组与其他类型转换
- C++中用sizeof()计算数组长度问题