#include
#include
using namespace std;
int main(void)
{
char *str = new char[100];
strcpy(str,"hello imooc");
cout << "*str";
delete[] str;
system("pause");
return 0;
}
有没有大神看下 ,小弟刚学c++这个代码运行后输出str不是输出100为什么
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答
- 林深 2016-05-04 16:05关注
如果是输出str内容的话或者是str字符串长度的话可以用以下方式:
#include "iostream" #include "string" #include "vector" using namespace std; int main(void) { char *str = new char[100]; strcpy(str,"hello imooc"); cout <<"str字符串内容是: " << str << endl; cout <<"str字符串长度是: " << strlen(str) << endl; delete[] str; return 0; }
如果你仅仅只有str这个字符指针的话,你想要获取到数组整体大小,很遗憾,这个str指针做不到,你要把str定义为数组类型的才行:
#include "iostream" #include "string" #include "vector" using namespace std; int main(void) { char str[100]; strcpy(str,"hello imooc"); cout <<"str字符串内容是: " << str << endl; cout <<"str字符串长度是: " << strlen(str) << endl; cout<<"str数组长度: "<< sizeof(str)/sizeof(char) <<endl; return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
- ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
- ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
- ¥15 谁有desed数据集呀
- ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
- ¥15 关于#hadoop#的问题
- ¥15 (标签-Python|关键词-socket)
- ¥15 keil里为什么main.c定义的函数在it.c调用不了
- ¥50 切换TabTip键盘的输入法
- ¥15 可否在不同线程中调用封装数据库操作的类