执著的专栏

Make it work, Make it right, Make it fast

C/C++ 指针的使用
#include<iostream>
using namespace std;

int main()
{
int *p =new int(2);
int *q = new int(3);
cout<<"before p = q"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;
p = q;     //p没有使用它本身分配的内存而是指向q
cout<<"after p = q"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;
delete q;     //删除后q指向的内存被释放
cout<<"after detete q (but not q=NULL)"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;

q = NULL;

return 0;

}

运行结果如下:


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/susidian/article/details/49908045
个人分类: C/C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

C/C++ 指针的使用

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭