首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于C++

2012-03-30 
关于C++求助代码如下:#include iostreamusing namespace stdint main(){char* srchelloint len s

关于C++求助
代码如下:
#include <iostream>
using namespace std;
int main()
{
char* src="hello";
int len = strlen(src);
char* p=new char[len];
strcpy(p,src);
delete []p;
  return 0;

}
本人才接触C++变成,所以很菜。

想问的是这段代码为什么在运行的时候出现内存错误。还请各位指教。

[解决办法]

C/C++ code
#include <iostream>using namespace std;int main(){    char* src="hello";    int len = strlen(src);    char* p=new char[len+1];//加个1就行了,因为strlen得出来的结果是不包含\0的,而字符串是以\0为结束标志    strcpy(p,src);    delete []p;    return 0;}
[解决办法]
char* p=new char[len+1];

热点排行
Bad Request.