33,311
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
int main()
{
char* p2;
p2="123456";
char* p="123456";
cout<<p<<endl<<p2<<endl;
return 0;
}
int main()
{
char* p2; //声明了p2指针,未分配内存
p2="123456"; //把p2 指向常量字符串,
char* p="123456"; //声明指针,同时把p2指向常量字符串,同样未分配内存
cout<<p<<endl<<p2<<endl;
// p[2]='2'; 因为这两个操作都是非法的
// p2[2]='2';
cout<<p[2]<<endl;
cout<<p2[2]<<endl;
system("pause");
return 0;
}
int main()
{
char* p2; //声明了p2指针,未分配内存
p2="123456"; //野指针操作
char* p="123456"; //声明指针,同时初始化数据,默认分配对应的内存
cout<<p<<endl<<p2<<endl;
return 0;
}