qq_34803835 2016-05-04 15:00 采纳率: 50%
浏览 1466
已采纳

有没有大神看下 ,小弟刚学c++这个代码运行后输出str不是输出100为什么

#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;
}

  • 写回答

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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料