简单程序如下:
#include <iostream>
#include <string>
#include <cstring>
#include "stdio.h"
#include "stdlib.h"
using namespace std;
int main(){
char* s="hello";
cout<<strlen(s)<<endl;
char* p=new char[strlen(s)];
cout<<strlen(p)<<endl;
char* p1=(char*)malloc(strlen(s));
printf("%d\n",strlen(p1));
return 0;
}
运行结果为:
5
22
22
现在,我就很奇怪了,用new或malloc去申请动态字符数组,明明申请的字符个数和s一样多,为什么strlen(p)或者strlen(p1)的时候输出22呢,而不是5!!??
慕容森
吃鸡游戏
相关分类