关闭

C/C++ 指针的使用

144人阅读 评论(0) 收藏 举报
分类:
#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;

}

运行结果如下:


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:96422次
    • 积分:1740
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条